Async Techniques and Examples in Python Transcripts
Chapter: Threads
Lecture: Demo: Waiting on more than one thread

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

Talk Python's Mastodon Michael Kennedy's Mastodon