from loguru import logger
import threading
import time
from concurrent.futures import ThreadPoolExecutor


pool = ThreadPoolExecutor(max_workers=100)

var = 0

lock=threading.Lock()


def foo():
    global var
    global lock
    with lock:
        if var < 10:
            time.sleep(2) # 模拟网咯 IO 操作,比如操作数据库,或者请求接口
            var += 1


for _ in range(100):
    pool.submit(foo) 

pool.shutdown(wait=True) # 等待所有任务执行完毕

logger.debug(f'最后的 {var}')

很简单,在提交完任务之后,只用 pool.shutdown(wait=True) 等待所有任务执行完毕

关于 wait 参数,可以看官方的代码注释:

def shutdown(self, wait=True, *, cancel_futures=False):
    """Clean-up the resources associated with the Executor.

    It is safe to call this method several times. Otherwise, no other
    methods can be called after this one.

    Args:
        wait: If True then shutdown will not return until all running
            futures have finished executing and the resources used by the
            executor have been reclaimed.
        cancel_futures: If True then shutdown will cancel all pending
            futures. Futures that are completed or running will not be
            cancelled.
    """
    pass
If True then shutdown will not return until all running futures have finished executing and the resources used by the executor have been reclaimed.

翻译一下就是:

如果是 "True",那么在所有正在运行的期货完成执行和执行器使用的资源被回收之前,关闭将不会返回。

universe_king
3.4k 声望678 粉丝