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}')

Very simple, after submitting the task, just use pool.shutdown(wait=True) to wait for all tasks to be executed

Regarding the wait parameter, you can see the official code comments:

 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.

Translate it to:

If "True", the shutdown will not return until all running futures have finished executing and resources used by the executor have been reclaimed.

universe_king
3.4k 声望678 粉丝