Async Techniques and Examples in Python Transcripts
Lecture: Demo: Waiting on more than one thread
0:00 There's a couple of things that are interesting here
0:01 that we might want to dig in deeper
0:03 really quickly before we get to a real project.
0:05 One is how do we work with say multiple threads?
0:09 So let's go down here and rename this to t1.
0:13 And then we'll have another one, t2.
0:19 And this is going to be Sarah, and this will be five.
0:25 And we're going to start both of those
0:28 and we're going to wait for them both to finish and then
0:33 we're going to be done.
0:35 This is sub optimal but just hang with me for a minute.
0:38 It's part of a journey. Alright look here we go.
0:42 Hello Michael, Sarah is in the work.
0:43 Hello, print, print, you can see the print
0:47 is actually somehow even not getting
0:49 the new line in there sometimes, that's pretty cool.
0:52 So Michael actually had more but here
0:54 Sarah, Michael, Sarah, Michael.
0:56 Pretty cool. So you can see that the, more time to sleep.
1:00 We're actually giving up our time slice
1:02 to let the other thread run.
1:04 That's pretty cool already.
1:06 So here's how we could do this.
1:08 What if we had 10 things we wanted to do?
1:10 What if we had arbitrary many? Not so great.
1:13 So let's do it like this and let's say all of our threads
1:15 are going to be in here, just put them into a list, okay.
1:20 This will be much nicer.
1:22 Put them in a list, and then we can do
1:25 a little list comprehension, t.start() for t in threads.
1:31 And down here, similarly t.join().
1:35 What do we get when we run those two comprehensions?
1:38 A bunch of Nones probably but it doesn't matter.
1:41 The fact is we're taking the return by here
1:45 which like I said is probably None
1:47 and just put it into a list but
1:49 as part of going to that list to generate it
1:51 we're of course waiting.
1:52 So now this will do exactly what we had before
1:55 and it lets us have even more of these.
2:02 Sorry Mark, 2, 11 obviously some commas in there.
2:07 Everything's going to go there they go.
2:11 A bunch of stuff is happening.
2:13 They're all cranking out answers.
2:14 Pretty soon we're going to be done.
2:17 But notice we're waiting for all of 'em.
2:18 We've started them all.
2:22 We're down here on line 18 waiting and now we're done.
2:24 Okay so this is a real nice pattern here.
2:26 This create a list of threads.
2:29 Used a list comprehension to start them
2:30 a list comprehension to wait on them and so on.
2:33 The final thing I guess worth looking at here
2:35 would be to say, come over here we could set a timeout.
2:40 So we're waiting right now forever
2:43 and let's say we're only willing to wait 5 seconds.
2:45 So maybe Sarah will be done.
2:47 Maybe Zoe, definitely Zoe will be done
2:49 but Michael and Mark, they won't be.
2:51 So let's say timeout is 5 seconds.
3:04 It went through all of them
3:06 because it first waited for that one which is 5
3:08 and then it waited on that one and that one.
3:11 Alright so let's make that, 'cause we're waiting on a bunch
3:14 to make that a little bit small and let's make it 1.
3:15 So now we're waiting for 4 seconds
3:17 I guess total probably.
3:23 Here you go. So you can see we got through basically 3
3:25 sorry 4 reps that are based of all of them
3:28 and then we just baled out.
3:29 So you may want to use that.
3:31 We're going to use that for good use in our little program
3:33 we're going to build in a moment.
3:35 Alright, so here's some cool techniques.
3:36 I guess I'll leave the timeout there for you
3:38 just in your code sample.
3:41 Good technique, you want to spin up a bunch of threads.
3:43 You want to start them, do the work potentially
3:47 and wait on them.
3:48 You can use a list and list comprehensions
3:50 to make this a lot cleaner in Python.