 Welcome back everyone. My name is Brian. And we're going to continue our journey with Python. And we're talking about Damon threads. Now what are these things? Are they demons? Are they possessed? What? What is a Damon? So basically in terms of computing this when you see this word Damon, it means it's something that runs and executes in the background but it's also controlled. We're going to quit a thread when the app quits. So if you've played around with threading and you kick off a thread and you quit the app, the thread's still going to run and we're going to demonstrate that in this video how it'll just run forever. I'm going to show you how to get around that. Okay, we're going to speed run right through this little part here. What we're going to do is imports and we're just going to do a little copy and paste action just because we've done these before. We're going to work with logging. We're going to configure logging in the main function. We're going to work with threading the entire module. And from threading, we're going to import thread and timer. That way I don't have to explicitly say threading dot thread or threading dot timer. And then import time that way we can actually stop the current thread test functions. We're going to have two of them here. So I'm going to say def test. And then we're going to go ahead and let's make another one called def. And stop is going to just literally say logging dot info. Just want to let the user know we are exiting the application and we're going to go ahead and say exit zero. So we're exiting as expected. We've talked about exit codes before. But basically we're telling Python we're done. We're done. Shut everything down. Now inside of test. This is where we're going to say thread name. We want to get the actual name of the thread. I can get the name there. And we're going to say logging info and we want to know what we're actually starting here. And we're just going to put the thread name in there. And then I can grab this little guy for the copy and paste fun stuff. We're not going to do anything kind of crazy here. All we're going to do is just simulate some sort of thread. So I'm going to say for X in a range. And we're going to say 60. Just to simulate long activity here. And I want to logging. Let's go ahead and say counting. Actually let's just say working. And then let's put this thread to sleep. We're just gonna sleep for one second. So if you're doing the math here, we're actually sleeping for 60 seconds because we're going to sleep one second intervals time 60. From there, I want to know, hey, we are now done. And there you go. Super simple little test function. But let's look at the two different ways of running this. All right, we're actually almost done here. So we're going to do our main function. And I'm going to do a fair amount of copy and paste just to speed this up because we've done this multiple, multiple times here. And I'm going to just grab some stuff off the screen bear with me. All right, so we're saying if name main, then call the main function. If we're in main, then we're going to configure our logger app start and then main thread finish. Actually, let's say main thread start. Now we can look at the real differences here. So first thing I want to do is stop in the seconds. So to do that, we're going to grab a timer. And we're going to call our stop function. We covered timers in a previous video. But basically what we're doing here is we're saying, hey, go ahead and kick up a timer. And in three seconds, we're going to call our stop function. In three seconds, go ahead and call the function stop. See timer three stops. So it's going to go up here and say exiting the application and it's going to exit. Go ahead and test that real quick. 123 and exiting the application. So that is working as expected. Let's make a normal thread. So I'm going to say t equal red. We want the target to be our test function. And we want to say Damon equals false now by default, this is false, but I'm just going to explicitly say false just so you can see. Let's go ahead and say t start. So this is kind of the default or the normal way. We are not going to run the Damon. Let's go ahead and clear this. I want you to be able to see this output and definitely watch. So main thread finished exiting the application. But look, our thread is still running. So our application thinks it's actually done, but this thread is just going to run forever and ever and ever. Oh, boy, that's bad. So let's go ahead and kill that. And now we're going to grab this. And we're just going to flip it to true. It's just literally just how simple it is. So if you ever have a thread that's running, and it goes past the application exit, you really just need to set Damon to true. Let's go ahead and demonstrate. So when we exit the application, it's going to go ahead and any thread that's running as a Damon is going to also exit as well. Really simple, really easy to use and simple to wrap your head around. This is one of the easier concepts and threading. Now, why would you want to shut your thread down when the application closes? Well, because it's expected behavior of an application to have a thread continue to run after an application has stopped is considered very bad and very harmful. And some people would actually consider it a security breach of some kind. I hope you enjoyed this video. You can find the source code out on github.com. If you need additional help, myself and thousands of other developers are hanging out in the void realms Facebook group. This is a large group with lots of developers and we talk about everything technology related, not just the technology that you just watched. And if you want official training, I do develop courses out on you to me.com. This is official classroom style training. If you go out there and the course you're looking for is just simply not there. Drop me a note. I'm either working on it or I will actually develop it. I will put a link down below for all three of those. And as always, help me help you smash that like and subscribe button. The more popular these videos become the more I'll create and publish out on YouTube. Thank you for watching.