Async Techniques and Examples in Python Transcripts
Chapter: Threads
Lecture: Demo: hello threads

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Time to write some code about threads isn't it? Alright, here we have our demo code. This is in the GitHub repository under Chapter 5 threads
0:09 we have basic threads, CPU attempt which we're going to get to these two shortly but right now I just want to focus on sort of
0:16 the Hello World thread stuff. What are the basics of threads? How do we create them, how do they work?
0:21 What are some of their little non-obvious nuances that we need to understand? And then we'll go build a real program with them.
0:27 Here you can see a program called Hello let's go ahead and run that. It's underwhelming isn't it? Didn't do too much.
0:32 It did run, but then it just passed. So let's go and do some interesting things. Let's have a method here, say def greeter.
0:42 I want to pass in name, it's going to be a string and the greeter, also let's pass in a times. And that could be an integer.
0:51 So we're going to pass in a name and a number times to do the greeting. So we'll just print out something like hello there whatever your name is.
1:00 And then we're going to do this that many times. And we'll go over here and call the greeter and let's say Michael. Let's do it 100 times.
1:10 Well that's interesting but let's make it go slower. Each time we're going to make it wait so we can say time.sleep().
1:17 This will actually put the main thread to sleep if it were another thread it would put that thread to sleep as well and we'll release the GIL
1:23 which is pretty awesome. Let's see if we can do this once per second. So this should take a little over a minute and a half.
1:29 There it goes. What do you think, shall we just watch it? No of course, that's going to be boring.
1:34 There's nothing to do with threads here anyway is there? So let's start bringing threads into action here.
1:40 So the first thing I'm going to do is I'm going to say import threading and keep it nice and explicit like I do even for my production code.
1:48 I like to use the module name the namespace style of programming here. So what we're going to do is we're going to create a thread
1:54 call it just t. I'm going to create threading.thread. What we're going to do is set the target. The target is going to be the function, not calling it
2:04 but just the function name. And then we're going to pass the args which are arguments as a tuple and you know we can sort of just decompose
2:12 this really easily and just put that there. Now if I run this it's not going to be so super interesting. Let's do a little print done here.
2:23 What happened to our greeter? Didn't it say anything? No, no no no it did not. Just because we created the thread
2:29 doesn't mean it's running. We have to say t.start(). There, wait a minute. It was done and now it's still working. That's weird. Here's the deal.
2:39 Our program, our main method, exited but somehow this other thread is still running. It's still, you can see it's still goin' on down here
2:46 doing it's thing. I'll finally stop it. That's because by default, threads in Python are what you would call foreground threads.
2:54 If any of those are running even if your main thread that started your program stops or exits, it's going to keep on running.
3:02 If you want this just to run in the background if your program exits then the threads get canceled then what we got to do is go here and say
3:09 this is a daemon thread. Now we'll get a slightly different behavior. Hello, nope, we're done. Oh, okay well, interesting, there it goes.
3:18 That wasn't really what you were hoping for maybe maybe it was. So we started our thread but somehow we need to do some other work.
3:25 We could say print, this is other work. Like for example I could do math. I could tell you what two times two is.
3:33 And then maybe after this point we don't have any more work to do but we want to make sure the thread still gets to finish its work
3:40 and just so we don't go insane let's make that 10 okay? 10 Michaels and we'll be good. And we could even do an in here.
3:50 And print that out so we can keep track of where we are. Okay so then down here if we say we don't want to exit we'll say t.wait, no, join.
4:02 So remember I spoke about this fork-join pattern the nomenclature here comes from fork-join. I would prefer wait and you'll see in other things
4:10 in the threading world that we're going to get to wait is sort of the newer style but, anyway. Join it is, let's go. So we got this Hello.
4:21 This is other work for, so remember computing for was our other work and now we're doing the other things. This is a zero-based loop, a computer loop
4:30 not a human loop. So we get this out here. So we sort of kicked off some work, this is other work and this is our main thread
4:37 our background thread is still working and then when it's finally done this line releases. And when it releases after it's done
4:45 'cause the thread stopped running we print our little final done and exit. So now our main method won't actually exit until the thread is done.
4:54 If it does because it's a daemon that background thread and background work will be immediately aborted.


Talk Python's Mastodon Michael Kennedy's Mastodon