如何重定向多处理池功能

时间:2017-08-04 06:36:41

标签: python tkinter

我创建了一个tkinter文本小部件,我想将sys.stdout重定向到文本小部件。我部分完成了这个功能,但是我遇到了多处理的问题。

以下是你可以玩的蟒蛇3玩具,

import sys
import tkinter as tk
from multiprocessing import Pool

root = tk.Tk()
root.geometry('600x400+30+30')

textArea = tk.Text(root, bg='gray', width=70, height=20)
textArea.pack()
tk.Button(root, text='click me', command=lambda: print('Dont touch\n me!')).pack()

class redirectStd():
    def __init__(self, textWidget):
        self.textBoard = textWidget
        self.flush = sys.stdout.flush
    def write(self, msg):
        self.textBoard.insert('end', msg)

sys.stdout = redirectStd(textArea)
sys.stderr = redirectStd(textArea)

def f(x):
    print('calculate:', x, '** 2')
    return x ** 2

def testParallel():
    print('start testing')
    with Pool(processes=2) as p:
        res = p.map(f, [1, 4, 0, 7, 3, 5])
        print(res)

tk.Button(root, text='test multiprocessing', command=testParallel).pack()

root.mainloop()

这将正确运行。但是,def f(x)中的打印件不会在文本小部件中打印任何内容。

如果self.flush = sys.stdout.flush阻止中的评论class redirectStd,则会导致 redirectStd 中出现 flush 错误的错误。在这个阶段,我还没有找到如何为 redirectStd 定义 flush 方法。完整的错误消息是

start testing
Exception in Tkinter callback
Traceback (most recent call last):
  File "/Users/zsf/miniconda3/lib/python3.6/tkinter/__init__.py", line 1699, in __call__
    return self.func(*args)
  File "test.py", line 28, in testParallel
    with Pool(processes=2) as p:
  File "/Users/zsf/miniconda3/lib/python3.6/multiprocessing/context.py", line 119, in Pool
    context=self.get_context())
  File "/Users/zsf/miniconda3/lib/python3.6/multiprocessing/pool.py", line 168, in __init__
    self._repopulate_pool()
  File "/Users/zsf/miniconda3/lib/python3.6/multiprocessing/pool.py", line 233, in _repopulate_pool
    w.start()
  File "/Users/zsf/miniconda3/lib/python3.6/multiprocessing/process.py", line 105, in start
    self._popen = self._Popen(self)
  File "/Users/zsf/miniconda3/lib/python3.6/multiprocessing/context.py", line 277, in _Popen
    return Popen(process_obj)
  File "/Users/zsf/miniconda3/lib/python3.6/multiprocessing/popen_fork.py", line 17, in __init__
    sys.stdout.flush()
AttributeError: 'redirectStd' object has no attribute 'flush'

任何人都可以为此提供一些解决方案吗?谢谢!

1 个答案:

答案 0 :(得分:0)

我认为您使用多线程的方式可能有问题,请查看下面的文档

https://docs.python.org/2/library/multiprocessing.html https://docs.python.org/2/library/multiprocessing.html#multiprocessing-programming

我以稍微不同的方式解决了你的问题,希望这有帮助

import sys
import tkinter as tk
from multiprocessing import Pool


class redirectStd():
    def __init__(self, textWidget):
    self.textBoard = textWidget
    #self.flush = sys.stdout.flush
def write(self, msg):
    self.textBoard.insert('end', msg)

def f(x):
    print('calculate:', x, '** 2')
    return x ** 2

if __name__ == '__main__':

    root = tk.Tk()
    root.geometry('600x400+30+30')

    textArea = tk.Text(root, bg='gray', width=70, height=20)
    textArea.pack()
    tk.Button(root, text='click me', command=lambda: print('Dont touch\n me!')).pack()

    sys.stdout = redirectStd(textArea)
    sys.stderr = redirectStd(textArea)

    def testParallel():
        p = Pool(5)
        print(p.map(f, [10, 2, 3]))

    tk.Button(root, text='test multiprocessing', command=testParallel).pack()

    root.mainloop()