Async Techniques and Examples in Python Transcripts
Chapter: Leveraging CPU cores with multiprocessing
Lecture: Multiprocessing return values

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So this multiprocessing
0:01 is great if we're going to kick off
0:03 a bunch of work and have it be done.
0:05 What if we need an answer though?
0:07 What if we need to get back some
0:08 kind of result from all of this work?
0:11 How do we do that?
0:12 It turns out multiprocessing has a pretty
0:14 good answer. Let's have a look.
0:17 I'm going to call this a task here
0:18 and what we're going to do is we can actually
0:20 capture the return value of a variable
0:23 that we can put them all in to.
0:27 I'm going to pin it like that and then
0:29 when we're done, we can also print out our results.
0:36 We can go to these tasks and we say t.get()
0:39 Notice we can pass a time out.
0:41 It will let us know whether it's ready.
0:44 We can wait on it, we can know whether
0:45 or not the response was actually successful.
0:48 Like the execution was successful and so on.
0:50 I'm going to do a get() and we'll print that out.
0:54 We're going to get, well, right now
0:55 nothing back because do_math() returns nothing.
0:57 But let's try and make it do something interesting.
1:00 So here we're generating a bunch of numbers.
1:02 Let's go over here and have an average
1:06 set that to zero and let's say the value
1:09 is that, so we'll say average going to add
1:11 the value and we're going to divide by the number.
1:15 We want it to return, just make this an integer
1:19 the average right there.
1:21 This is not really a meaningful number
1:22 we're computing, but we're going to return
1:24 that value from each block of the run
1:27 that we're working with.
1:28 Let's go ahead and run that, then I'm going
1:30 to go to something called the get() function.
1:31 That will return the value, returned
1:33 on line 40 there. Let's run it and see what happens.
1:37 Doing all the work like before
1:38 and boom, look at that. Those are the answers.
1:41 Here are our results. I got those back from the various
1:44 functions that ran. Of course we got them back in the order
1:48 that we started them, not in the order
1:50 in which they finished.
1:52 Because we start item one, put it in the list.
1:56 Start item two, put it in the list and so on.
1:59 Then we just, when everything's finished
2:01 we go through all the items first from the list
2:03 second in the list and get the results.
2:05 Correlate those back really clearly.
2:07 This is the first block, the second block
2:09 and third block of data that we're working with.
2:12 That's how you get results back or even
2:14 understand whether or not the operation
2:16 was successful when using multiprocessing.