My problem is the following: I have a multiprocessing.pool.ThreadPool
object with worker_count
workers and a main pqueue
from which I feed tasks to the pool.
The flow is as follows: There is a main loop that gets an item of level
level from pqueue
and submits it tot the pool using apply_async
. When the item is processed, it generates items of level + 1
. The problem is that the pool accepts all tasks and processes them in the order they were submitted.
More precisely, what is happening is that the level 0
items are processed and each generates 100 level 1
items that are retrieved immediately from pqueue
and added to the pool, each level 1
item produces 100 level 2
items that are submitted to the pool, and so on, and the items are processed in an BFS manner.
I need to tell the pool to not accept more than worker_count
items in order to give a chance of higher level to be retrieved from pqueue
in order to process items in a DFS manner.
The current solution I came with is: for each submitted task, save the AsyncResult
object in a asyncres_list
list, and before retrieving items from pqueue
I remove the items that were processed (if any), check if the length of the asyncres_list
is lower than the number of threads in the pool every 0.5 seconds, and like that only thread_number
items will be processed at the same time.
I am wondering if there is a cleaner way to achieve this behaviour and I can't seem to find in the documentation some parameters to limit the maximum number of tasks that can be submitted to a pool.