Async Techniques and Examples in Python Transcripts
Chapter: Common APIs with execution pools
Lecture: Demo: Executor app (process-edition)

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So you've seen that we've taken our little app that goes against these titles from these URLs using threads via the ThreadPoolExecutor
0:09 to do all the work. So we want to switch from maybe using threads to using processes, using multiprocessing.
0:17 How much work is that going to be? Watch this. We're going to go over here and say, you know what? We're going to go from process, ProcessPoolExecutor.
0:27 Now because we imported it as an alias down here, this line on line 19, doesn't have to change. Before we added line five, that meant
0:36 create a ThreadPoolExecutor. Now we've changed what that means now it means create a ProcessPoolExecutor. I'll go ahead and comment this out
0:43 even though, technically it'll work the same. Now what happens if we run it? Once again, let's make that bigger.
0:50 So it's running. You can see the same basic process. We kick off all of the work. We wait for a second to get all of the results.
0:59 Beautiful, and we do it all in parallel. Notice now though, now the process ID is different. The process ID is different for each one of the these
1:08 and it says process name is fork process one fork process two, fork process three, and so on. Even though we're calling across these processes
1:16 we're able to get a return value back by just getting access to the result. So run it one more time. Boom, quite nice, right? So look at that.
1:26 To create the process pool edition instead of the threaded pool edition we change line four to line five. That's it because we can use this base API
1:36 that is this executor API, and these futures that come back regardless of what type of work they're doing, it means we can use this same API
1:44 for multiprocessing and for threading. Pretty awesome.


Talk Python's Mastodon Michael Kennedy's Mastodon