Python concurrent 包
1、concurrent.futures
异步并行任务编程模块,提供一个高级的异步可执行的便利接口。
提供了两个池执行器:
ThreadPoolExecutor
异步调用的线程池的ExecutorProcessPoolExecutor
异步调用的进程池的Executor
2、ThreadPoolExecutor
ThreadPoolExecutor(max_workers=None)
# 池中至多创建max-workers个线程的池来同时异步执行,返回Executor实例
submit(*args, **kwargs)
# 提交执行的函数及其参数,返回Future类的实例
shutdown(wait=True)
# 清理池
# concurrent.futures
# ThreadPoolExecutor
from concurrent.futures import ThreadPoolExecutor
import time
import logging
import threading
FORMAT = "%(asctime)s %(threadName)22s %(thread)8d %(message)s"
logging.basicConfig(level=logging.INFO, format=FORMAT)
def worker(n):
logging.info('enter thread ~~~~~~~~~~~~~~')
time.sleep(n)
logging.info('finished ~~~~~~~~~~~~')
return n
executor = ThreadPoolExecutor(max_workers=3)
fs = []
for i in range(6):
future = executor.submit(worker, i) # 异步,非阻塞,执行完毕后立马执行下一条命令
logging.info(future)
fs.append(future)
# logging.info('-' * 55)
# for i in range(3):
# future = executor.submit(worker, 5) # 异步,非阻塞,执行完毕后立马执行下一条命令
# logging.info(future)
# logging.info('=' * 55)
# executor.shutdown()
# logging.info('*' * 55)
while True:
flag = True
for f in fs:
logging.info(f.done())
flag = flag and f.done()
if flag:
executor.shutdown()
logging.info(threading.enumerate())
break
# if threading.active_count() == 1:
# logging.info(threading.enumerate())
# logging.info(future)
# break
time.sleep(1)
logging.info(threading.enumerate())
for f in fs:
logging.info(f.result())
2022-04-21 16:01:17,590 ThreadPoolExecutor-0_0 5212 enter thread ~~~~~~~~~~~~~~
2022-04-21 16:01:17,590 MainThread 7416 <Future at 0x1fb22ca8730 state=running>
2022-04-21 16:01:17,590 ThreadPoolExecutor-0_0 5212 finished ~~~~~~~~~~~~
2022-04-21 16:01:17,590 ThreadPoolExecutor-0_0 5212 enter thread ~~~~~~~~~~~~~~
2022-04-21 16:01:17,590 MainThread 7416 <Future at 0x1fb22ca8c70 state=running>
2022-04-21 16:01:17,590 MainThread 7416 <Future at 0x1fb22ca8fa0 state=pending>
2022-04-21 16:01:17,591 ThreadPoolExecutor-0_1 31892 enter thread ~~~~~~~~~~~~~~
2022-04-21 16:01:17,591 ThreadPoolExecutor-0_2 49012 enter thread ~~~~~~~~~~~~~~
2022-04-21 16:01:17,591 MainThread 7416 <Future at 0x1fb22cb6040 state=running>
2022-04-21 16:01:17,591 MainThread 7416 <Future at 0x1fb22cb6400 state=pending>
2022-04-21 16:01:17,591 MainThread 7416 <Future at 0x1fb22cb64f0 state=pending>
2022-04-21 16:01:17,591 MainThread 7416 True
2022-04-21 16:01:17,591 MainThread 7416 False
2022-04-21 16:01:17,591 MainThread 7416 False
2022-04-21 16:01:17,591 MainThread 7416 False
2022-04-21 16:01:17,591 MainThread 7416 False
2022-04-21 16:01:17,591 MainThread 7416 False
2022-04-21 16:01:18,593 ThreadPoolExecutor-0_0 5212 finished ~~~~~~~~~~~~
2022-04-21 16:01:18,593 MainThread 7416 [<_MainThread(MainThread, started 7416)>, <Thread(ThreadPoolExecutor-0_0, started daemon 5212)>, <Thread(ThreadPoolExecutor-0_1, started daemon 31892)>, <Thread(ThreadPoolExecutor-0_2, started daemon 49012)>]
2022-04-21 16:01:18,593 ThreadPoolExecutor-0_0 5212 enter thread ~~~~~~~~~~~~~~
2022-04-21 16:01:18,593 MainThread 7416 True
2022-04-21 16:01:18,594 MainThread