[英]Using apply_async with callback function for a pool of processes
I am trying to understand how multiprocess pools work.我试图了解多进程池的工作原理。 In the following programing I created a pool of 4 processes.在下面的编程中,我创建了一个包含 4 个进程的池。
And I call apply_async
with a callback function that should update a list called result_list
我用一个回调函数调用apply_async
,该函数应该更新一个result_list
apply_async
的列表
import Queue
from multiprocessing import Process
from multiprocessing import Pool
result_list = []
def foo_pool(q): #Function for each process
print "foo_pool"
if(q.qsize() > 0):
number = q.get()
return number * 2
def log_result(result):
# This is called whenever foo_pool(i) returns a result.
# result_list is modified only by the main process, not the pool workers.
result_list.append(result)
if __name__ == "__main__":
q = Queue.Queue()
for i in range(4):
q.put(i + 1) #Put 1..4 in the queue
p = Pool(4)
p.apply_async(foo_pool, args = (q, ), callback = log_result)
I realize I don't need to use a queue here.我意识到我不需要在这里使用队列。 But I am testing this for another program which requires me to use a queue.但是我正在为另一个需要我使用队列的程序测试这个。 When I run the program, the function foo_pool is not being called.当我运行程序时,函数 foo_pool 没有被调用。 The print statement print "foo_pool"
does not execute.不执行打印语句print "foo_pool"
。 Why is this?为什么是这样?
Roughly speaking, apply_async only schedule async task, but not run it.粗略地说,apply_async 只调度异步任务,但不运行它。 You need to call p.close()
and p.join()
to trigger execution or r = p.apply_async()
and r.get()
.您需要调用p.close()
和p.join()
来触发执行或r = p.apply_async()
和r.get()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.