import concurrent.futures
import time
def process_one(i):
try:
print("dealing with {}".format(i))
time.sleep(50)
print("{} Done.".format(i))
except Exception as e:
print(e)
def process_many():
with concurrent.futures.ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor:
executor.map(process_one,
range(100),
timeout=3)
if __name__ == '__main__':
MAX_WORKERS = 10
try:
process_many()
except Exception as e:
print(e)
根据文档:
如果在调用
Executor.map()
时,调用__next__()
方法后结果仍未返回,则迭代器将抛出concurrent.futures.TimeoutError
超时异常,等待时间为timeout
秒。
但是,在这里脚本没有抛出任何异常并持续等待。有什么建议吗?
process_many
调用时间控制在约3秒以内? - arachnivore