Python's thread pool uses a producer-consumer model

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

pool = ThreadPoolExecutor(max_workers=10)


def func_get():
    response = requests.get('http://127.0.0.1:5002')
    time.sleep(10)


def func_callback():
    pass


for i in range(100):
    feature = pool.submit(func_get)
    feature.add_done_callback(func_callback)

When other threads call the submit interface of the pool's queue, the task will be submitted to the pool's queue

The work thread will go to the task from the queue. If the queue is empty, it will block and hang.

 from concurrent.futures import ThreadPoolExecutor


pool = ThreadPoolExecutor(max_workers=10)
pool._work_queue.qsize()

You can use _work_queue to access this internal queue

References:
Does the callback function of Python's thread pool run in the main thread or the worker thread?


universe_king
3.4k 声望680 粉丝