from concurrent.futures import ThreadPoolExecutor
import time
pool = ThreadPoolExecutor(10, 'Python')
def fun():
time.sleep(1)
print(1, end='')
if __name__ == '__main__':
# 列表推导式
[pool.submit(fun) for i in range(20) if True]
from concurrent.futures import ThreadPoolExecutor
import time
pool = ThreadPoolExecutor(10, 'Python')
def fun(arg1,arg2):
time.sleep(1)
print(arg1, end=' ')
print(arg2, end=' ')
if __name__ == '__main__':
# 列表推导式
[pool.submit(fun,i,i) for i in range(20) if True]
# 单个线程的执行
task = pool.submit(fun,'Hello','world')
# 判断任务执行状态
print(f'task status {task.done()}')
time.sleep(4)
print(f'task status {task.done()}')
# 获取结果的函数是阻塞的,所以他会等线程结束之后才会输出
print(task.result())
# 等待线程全部执行完毕
wait(pool.submit(fun,1,2),return_when=ALL_COMPLETED)
print('')