In sync code, you might use
a thread pool and imap_unordered():
``` pool = multiprocessing.dummy.Pool(2)
for result in pool.imap_unordered(do_stuff, things_to_do): print(result) ```
Here, concurrency is limited by the fixed number of threads.
In sync code, you might use
a thread pool and imap_unordered():
``` pool = multiprocessing.dummy.Pool(2)
for result in pool.imap_unordered(do_stuff, things_to_do): print(result) ```
Here, concurrency is limited by the fixed number of threads.
Tips
lambda
or functools.partial
.max_worker = 1
is a very nice way to get a poor man’s task queue.Process pools are good for:
Thread pools are good for:
E.G: a web scraper, a GUI to zip files, a development server, sending emails without blocking web page rendering, etc.
Python standard library comes with a beautiful abstraction for them I see too few people use: the pool executors.