Async Techniques and Examples in Python Transcripts
Lecture: Concept: Timeouts
0:00 Let's quickly review this timeout operation that we did.
0:04 So here we have some threads
0:05 and we've started all the threads.
0:07 What we'd like to do is, if we get some kind of signal
0:09 and like I said, this can be many, many different things.
0:12 A Boolean variable should exit is set to true
0:15 or a value set in the database or you've received an email
0:19 I don't know, it could be anything.
0:21 I want to let these threads run until I get that message
0:24 and then I'm going to bail out of them.
0:27 So far we've been able to make them stop
0:30 by basically just letting our process exit.
0:33 If this was like a web service
0:34 or something that was alive for a long time
0:36 you'd have to abort the various threads.
0:37 So keep that in mind. So here we have a while loop
0:40 and we're saying, while any of the threads are still working
0:43 we're going to do a quick join on them
0:46 and that let's us say real quick, hey, wait for a little bit
0:51 and then we're going to do our check.
0:53 Now we're going to ask the question, are you alive?
0:55 And then basically use join to put our thread to sleep.
0:58 We could just as well do a time.sleep(), I suppose
1:01 but this one will cancel quicker
1:03 if all the threads are basically finished.
1:07 And then I want to check
1:08 and we just keep going around and around
1:09 we'd break out of this loop whenever we decide
1:12 we're done waiting, we want to cancel out.
1:14 If we don't break out
1:15 then the threads are going to run to completion.
1:17 That's a simple time out pattern
1:19 that we can use in Python threads.