"""进程池 回调函数 回调函数会把任务函数的结果作为参数执行"""
from multiprocessing import Pool
import time
import os
def work():
time.sleep(1)
print("%d 进程 work" % os.getpid())
return time.time()
def call(res):
# time.sleep(1)
print("处理回调结果:{}".format(str(res)))
if __name__ == '__main__':
pool = Pool(2)
for i in range(5):
# 在进程池中的每个任务执行完毕时,执行回调函数
# 使用callback回调函数时,会把工作任务func方法的返回结果传给callback方法进行处理
res = pool.apply_async(func=work, callback=call)
pool.close() # 必须等进程池关闭后,才能使用join()方法
pool.join() # 主进程等待子进程结束
print("main end")
Python 进程池 回调函数
最新推荐文章于 2024-05-13 11:26:39 发布