【发布时间】:2014-08-08 13:24:31
【问题描述】:
我有一个带有 GUI 的程序需要进行一些多处理。这样做的目的是避免冻结 GUI,并允许用户在进程运行时使用其他一些按钮。
然后我想定义如下方法:
def start_waiting(self, parent, func, args):
self.window_waiting.show()
task=Process(func(*args))
task.start()
task.join()
# Some code to execute at the end of the process
#
#...
问题是join() 不起作用,我需要它,因为join() 之后执行的代码表明Process 何时结束。我将使用该代码更新window_waiting 的取消按钮。
然后我想到了另一个避免使用join() 的解决方案,我将其替换为:
while task.is_alive():
time.sleep(0.5)
但这两个都不起作用,所以我尝试了一个方案 C,即创建一个队列:
def worker(input, output):
for func, args in iter(input.get, 'STOP'):
result=func(*args)
output.put(result)
task_queue = Queue()
done_queue = Queue()
task_queue.put(task)
Process(target=worker, args=(task_queue, done_queue)).start()
done_queue.get()
最后一个代码给了我错误:'Pickling an AuthenticationString object is '
TypeError: Pickling an AuthenticationString object is disallowed for security reasons
这导致我到multiprocessing.Process subclass using shared queue,但我还没有设法解决问题:/
【问题讨论】:
-
The problem is that the join() function is not working怎么样? -
@Colonel 函数join()之后的代码在
taskfinish之前执行 -
(我很难理解这是因为那是 join() 函数的作用)
-
具体一点永远不会有坏处。它也可能引发错误或其他问题。
-
@Colonel 是的,你是对的 :)
标签: python python-2.7 multiprocessing gtk3 pygobject