Skip to content
Advertisement

How to create a continuous stream of Python’s concurrent.futures.ProcessPoolExecutor.submits()?

I am able to submit batches of concurrent.futures.ProcessPoolExecutor.submits() where each batch may contain several submit(). However, I noticed that if each batch of submits consumes a significant about of RAM, there can be quite a bit of RAM usage inefficiencies; need to wait for all futures in the batch to be completed before another batch of submit() can be submitted.

How does one create a continuous stream of Python’s concurrent.futures.ProcessPoolExecutor.submit() until some condition is satisfied?

Test Script:

JavaScript

Advertisement

Answer

To expand on my comment, how about something like this, using the completion callback and a threading.Condition? I took the liberty of adding a progress indicator too.

EDIT: I refactored this into a neat function you pass your desired concurrency and queue depth, as well as a function that generates new jobs, and another function that processes a result and lets the executor know whether you’ve had enough.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement