from concurrent.futures import as_completed,ProcessPoolExecutor
from concurrent.futures.thread import ThreadPoolExecutor
import random
import threading
import time
import concurrent
def rand_sleep(i):
# print(threading.currentThread().getName())
# time.sleep(random.randint(1,10))
time.sleep(i)
print(i)
return i
if __name__ == "__main__":
# with ThreadPoolExecutor(max_workers=3) as ex:
with ThreadPoolExecutor(max_workers=3) as ex:
tasks = [ex.submit(rand_sleep,i) for i in range(10)]
task = tasks[0]
try:
for task in as_completed(tasks,timeout=5):
result = task.result()
except Exception as e:
print(e)
print("for over")
# ex.shutdown(wait=False)
ex._threads.clear()
concurrent.futures.thread._threads_queues.clear()
print("progam over")
python 超时任务结束方式
最新推荐文章于 2024-04-29 06:58:28 发布