标签:回调 pre turn print result time() == 执行 code
import os
import sys
import time
from threading import Thread
from multiprocessing import Process
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
# t = ThreadPoolExecutor(os.cpu_count())
# p = ProcessPoolExecutor(os.cpu_count())
executor = ProcessPoolExecutor(max_workers=os.cpu_count())
def foo(a, b):
time.sleep(1)
print(f"foo end: {a}{b}...")
return a, b
def boo(obj):
res = obj.result()
print(f"boo.. {res}")
if __name__ == ‘__main__‘:
start_time = time.time()
for i in range(3):
res = executor.submit(foo, "Elton", i)
print(res.done()) # 是否执行完成
res.add_done_callback(boo) # 添加回调函数
print("主进程")
end_time = time.time()
print(f"Time consuming {end_time - start_time}")
"""
False
False
False
主进程
Time consuming 0.013116836547851562
foo end: Elton1...
foo end: Elton0...
boo.. (‘Elton‘, 0)
foo end: Elton2...
boo.. (‘Elton‘, 1)
boo.. (‘Elton‘, 2)
"""
标签:回调 pre turn print result time() == 执行 code
原文地址:https://www.cnblogs.com/wangcheng9418/p/13358350.html