Choosing ThreadPoolExecutor or ProcessPoolExecutor
Now that you’ve seen how to run code in threads or processes using ThreadPoolExecutor and ProcessPoolExecutor, which should you choose to use at any given time?
Let’s revisit the table we saw earlier in this chapter:
Module | Paradigm | Object | Share Memory? | Restricted by GIL? |
---|---|---|---|---|
threading | threads | Thread | Yes | Yes |
multiprocessing | processes | Process | No | No |
The two primary differences between Thread and Process objects have to do with how memory is shared and how something called the GIL restricts performance.
First, we’ll discuss memory sharing in Thread and Process objects.
Investigating Memory Sharing in Threads and Processes
In general, if you use the Thread object from the threading module, your “threaded” code will ...
Get Intuitive Python now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.