haha, use to have the same problem.
try to use the concurrent.futures.ThreadPoolExecutor
along with as_completed
to deal with it.
Use ThreadPoolExecutor
to instantiate a thread pool object. Pass the max_workers
parameter to set the maximum number of threads that can run concurrently in the thread pool.
you can use such as submit
,cancel
,result
to check if a task is finished,
they require constantly polling in the main thread, which can be inefficient. Sometimes, we only need to know when a task finishes in order to fetch its result, rather than continuously checking each task. This is where the as_completed()
method comes in handy, as it allows retrieving the results of all tasks once they are completed. The as_completed()
method is a generator that blocks until a task is completed. When a task finishes, it yields the task, allowing the main thread to process it. After processing, the generator will continue blocking until all tasks are finished. As shown in the results, tasks that complete first will notify the main thread first.