Python Threading Explained in 8 Minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on guys welcome back in this video today we're going to cover multi-threading in python as simply and as quickly as possible so let us get right into it [Music] all right now one thing that I want to mention right away is that there is no actual multi-threading in Python at least not in the most commonly used C python implementation that you're probably running on your system because of something called the global interpreter lock and we're not going to go into too much detail here but what this essentially means is that we have some sort of pseudo multi-threading where we do stuff concurrently and somewhat at the same time but what's actually happening is that we're switching between the tasks and we're using the down times of the different tasks to execute the other tasks so for example we send a request we wait for a response that is some down time or we're waiting for user input that is some downtime or we're just sleeping with time.sleep that is also some downtime and this can be used for the execution of other tasks and this is what the threading module actually does and this is just the theoretical background here in order to work with multi-threading in Python with the pseudo multi-threading in Python what you need to do is you need to say import threading this is a core python module we don't need to install anything for that and in addition to that we're going to also import time we're just going to allow us to use the mentioned sleep function to create some downtime and we're going to do a very simple example here to understand the concept of threading in python or multi-threading in Python what we're going to do is we're going to define a function we're going to call it worker because it's going to be our worker function and this function will do something quite simple we're going to Define a variable up here done we're going to set it to false and inside of our worker function we're going to have a loop while not done so basically an endless loop until this thing is set to true while not done we're going to say uh time dot sleep wait for one second sleep one second basically and then we're going to print some counter we're going to first of all Define a counter up here being equal to zero we're going to say that the counter is going to be increased by one with each iteration and we're going to print the counter this is what we want to do here nothing too fancy just a simple function that constantly prints one two three four until we set done to true so without using any threading stuff here let's just run this function and see what happens this is going to be then running in the main thread and you can see it just prints one two three four five and so on um now what we want to do here is we want to in addition to that function also process some user inputs so in the main thread for example we want to wait for the user to input something or to just press enter now how would we do that we would do something like while not done we want to say uh wait for the user input so press enter to quit for example or actually we don't need to Loop here we can just wait for the input statement here and after the input statement is over so after the user presses enter we can just set done to true so the goal is to have this function running counting from one to two to three to four and so on until we press enter to quit that the problem is however now that um this is running in the main thread and this comes after this function so we will not get to that statement unless this function finishes with the work so unless this function breaks out of the loop and we can continue with the rest of the code if we want to have both things running simultaneously we will have to do multi-threading and this has done the following way we use the threading module to define a thread by saying threading dot threat and then we can specify a Target function so Target equals and we can pass the worker function here as an object passing it as an object means not passing it with the parentheses so not calling it but just passing the instance of worker just passing the object worker which is the function The Entity worker um and then this is the object this is the actual threat we can either store it in a variable and then start a variable or we can start it right away like this by setting as saying threading dot thread Target equalsworker dot start and this is going to now start the worker function in a separate thread so if I run this um you will be able to see press enter to quit one two three I'm not pressing anything if I now press enter it goes past the statement and it says done to true and since done is set to true this also terminates this worker now this would not work of course if I have here an endless loop while true then even if I press enter it's not going to quit because then the main thread doesn't have anything to do but this thread is still running however we can also change that by saying that this thread is not an important threat we don't want to keep the program running if everything else has finished this is a so-called Daemon threat which basically means if nothing else is running you can also quit the script even though this thread is running we can specify here Daemon equals true and this basically means what I just said that this is running in the background and as soon as the main thread and the other important threads have finished we can also quit the script we don't have to wait for this particular thread here because it's just a Daemon thread so I can run this now and I can press enter And even though this enter doesn't change anything about this Loop even though this thread is still running we're just terminating the whole script because this is just a Daemon threat that is what this is for we can also pass arguments by just passing text here for example and saying that we want to print an F string with text and then the counter but just making up some stuff here uh we can do that by passing arguments so we can say arcs equals and we can pass a tuple of arguments here and in this case we're going to just pass ABC but since this is a tuple we need to also pass a comma otherwise this is just going to be treated as a single value but we need a tuple we can do the same thing we can run another thread with the same function x y z here as a text and you're going to see what this actually does you can see that it prints ABC counter oh actually I just noticed this print encounter we need of course to print the actual value of counter you can see here now abc1 xyz1 uh how these are running in parallel and if I press enter since both of them are Daemon threats we are quitting the script if I set now one of them not to a Daemon threat and the other one uh to a Daemon threat uh you will see that if I just press enter now one of them is going to stop um actually doesn't stop any of those oh of course sorry this was uh this was not a correct thought because since one of them is running the other one is going to run as well because if we set one of them to not a Daemon thread this is still an important threat and because the script is not finished because there is some thread still running the Daemon thread is going to run as well um so setting one of them to false to not being a Daemon threat keeps all of them running because Daemon doesn't mean that you're just quitting it but it just means that since something is still running we're going to keep this running as well but if nothing else is running we're going to not keep this running anymore and the last thing I want to show you here I'm not going to show you here as a demonstration but just as a concept is if you have a list of multiple threats if you say for example this is T1 and this is T2 and you don't call the start but you just save them as objects here and then you do T1 start and T2 star this is the same functionality here you can also wait for those threads so if you do something and you don't want to continue with this here until both threads have finished with the execution then you can say t1.join and t2.join and you would not get to this code section here until the threads have finished so you can see it doesn't even print press enter to quit because it doesn't get to that line since both threads are still running so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you much for watching see you in the next video and bye [Music]
Info
Channel: NeuralNine
Views: 131,925
Rating: undefined out of 5
Keywords: python, threading, multithreading, multi-threading, crash course, multiprocessing, multi-processing, python multithreading, python threading, threads, concurrency, asycio
Id: A_Z1lgZLSNc
Channel Id: undefined
Length: 8min 38sec (518 seconds)
Published: Tue Jan 10 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.