标签:result 同步 time 一个 return 调用 结束 最大 class
使用线程池或进程池控制程序产生的进程数或线程数,来防止操作系统受到太大的压力从而导致服务器宕机
from concurrent.futures import ProcessPoolExecutor
import time
def run():
print(123)
time.sleep(0.5)
print(234)
return "我是最终结果"
def call_back(future):
print(future.result())
if __name__ == '__main__':
pool = ProcessPoolExecutor(3)
for i in range(10):
future = pool.submit(run)
future.add_done_callback(call_back)
pool.shutdown()
print("用了shutdown以后我最后在打印")
ProcessPoolExecutor
from concurrent.futures import ThreadPoolExecutor
import time
def run():
print(123)
time.sleep(4)
print(234)
def call_back(future):
print(future.result())
if __name__ == '__main__':
pool = ThreadPoolExecutor(3)
for i in range(10):
future = pool.submit(run)
future.add_done_callback(call_back)
pool.shutdown()
print("用了shutdown以后我最后在打印")
ThreadPoolExecutor
XMind: ZEN - Trial Version
标签:result 同步 time 一个 return 调用 结束 最大 class
原文地址:https://www.cnblogs.com/marklijian/p/11575226.html