Async Techniques and Examples in Python Transcripts
Chapter: Threads
Lecture: Concept: Timeouts

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's quickly review this timeout operation that we did. So here we have some threads and we've started all the threads.
0:08 What we'd like to do is, if we get some kind of signal and like I said, this can be many, many different things.
0:13 A Boolean variable should exit is set to true or a value set in the database or you've received an email I don't know, it could be anything.
0:22 I want to let these threads run until I get that message and then I'm going to bail out of them. So far we've been able to make them stop
0:31 by basically just letting our process exit. If this was like a web service or something that was alive for a long time
0:37 you'd have to abort the various threads. So keep that in mind. So here we have a while loop
0:41 and we're saying, while any of the threads are still working we're going to do a quick join on them
0:47 and that let's us say real quick, hey, wait for a little bit and then we're going to do our check. Now we're going to ask the question, are you alive?
0:56 And then basically use join to put our thread to sleep. We could just as well do a time.sleep(), I suppose but this one will cancel quicker
1:04 if all the threads are basically finished. And then I want to check and we just keep going around and around
1:10 we'd break out of this loop whenever we decide we're done waiting, we want to cancel out. If we don't break out
1:16 then the threads are going to run to completion. That's a simple time out pattern that we can use in Python threads.

Talk Python's Mastodon Michael Kennedy's Mastodon