from concurrent.futures import ProcessPoolExecutor, as_completed, ThreadPoolExecutor
def func1(a, b):
print(f"func1 start...")
return {"k": a + b}
def func2(c):
print(f"func2 start...")
return {"k1": c}
def run():
d = {}
res = []
with ThreadPoolExecutor(max_workers=4) as p:
# with ProcessPoolExecutor(max_workers=4) as p:
res.append(p.submit(func1, 2, 3)) # 异步执行
res.append(p.submit(func2, 55))
for r in as_completed(res): # 等待所有函数执行完成
if r.done() and r.result():
d.update(r.result())
print(d)
if __name__ == '__main__':
run()
python ProcessPoolExecutor 和ThreadPoolExecutor使用
最新推荐文章于 2023-10-30 14:59:36 发布