[0:00] what is going on guys welcome back in [0:01] this video today we're going to cover [0:03] multi-threading in python as simply and [0:05] as quickly as possible so let us get [0:07] right into it it's not a g it's [0:12] [Music] [0:16] AED all right now one thing that I want [0:18] to mention right away is that there is [0:20] no actual multi-threading in Python at [0:22] least not in the most commonly used [0:24] cpython implementation that you're [0:25] probably running on your system because [0:28] of something called the global interpret [0:30] to lock and we're not going to go into [0:31] too much detail here but what this [0:33] essentially means is that we have some [0:35] sort of pseudo multi-threading where we [0:37] do stuff concurrently and somewhat at [0:39] the same time but what's actually [0:41] happening is that we're switching [0:43] between the tasks and we're using the [0:44] down times of the different tasks to [0:46] execute the other tasks so for example [0:48] we send a request we wait for a response [0:51] that is some doubt time or we're waiting [0:52] for user input that is some downtime or [0:55] we're just sleeping with time dos sleep [0:57] that is also some downtime and this can [0:58] be used for execution of other tasks and [1:01] this is what the threading module [1:02] actually does and this is just the [1:04] theoretical background here in order to [1:06] work with multi-threading and python [1:08] with the pseudo multi-threading in [1:10] Python what you need to do is you need [1:11] to say import threading uh this is a [1:14] core python module we don't need to [1:15] install anything for that uh and in [1:18] addition to that we're going to also [1:19] import time which is going to allow us [1:21] to use the mentioned sleep function to [1:23] create some downtime and we're going to [1:26] do a very simple example here to [1:27] understand the concept of threading in [1:29] python or for multi-threading in Python [1:31] what we're going to do is we're going to [1:32] define a function we're going to call it [1:34] worker because it's going to be our [1:36] worker function and this function will [1:38] do something quite simple we're going to [1:40] Define a variable up here done we're [1:42] going to set it to false and inside of [1:45] our worker function we're going to have [1:47] a loop while not done so basically an [1:49] endless loop until this thing is set to [1:52] True while not done we're going to say [1:56] uh time do sleep wait 1 second sleep 1 [2:00] second basically and then we're going to [2:03] print some counter we're going to first [2:06] of all Define a counter up here being [2:08] equal to zero we're going to say that [2:11] the counter is going to be increased by [2:12] one with each [2:14] iteration and we're going to print the [2:16] counter this is what we want to do here [2:18] nothing too fancy just a simple function [2:20] that constantly prints 1 2 3 4 until we [2:24] set done to true so without using any [2:27] threading stuff here let's just run this [2:29] function and see what happens this is [2:31] going to be then running in the main [2:32] thread and you can see it just prints 1 [2:34] 2 3 4 5 and so on um now what we want to [2:39] do here is we want to in addition to [2:41] that function also process some user [2:43] input so in the main thread for example [2:45] we want to wait for the user to input [2:47] something or to just press enter now how [2:50] would we do that we would do something [2:52] like while not [2:53] done we want to say uh wait for the user [2:57] input so press PR enter to quit for [3:02] example or actually we don't need to [3:04] Loop here we can just wait for the input [3:05] statement here and after the input [3:07] statement is over so after the user [3:09] presses uh enter we can just set done to [3:12] true so the goal is to have this [3:14] function running counting from uh 1 to [3:17] two to three to four and so on until we [3:19] press enter to quit that the problem is [3:22] however now that um this is running in [3:25] the main thread and this comes after dys [3:28] function so we will not get to that [3:29] statement uh unless this function [3:32] finishes with the work so unless this [3:34] function breaks out of the loop and we [3:35] can continue with the rest of the code [3:37] if we want to have both things running [3:39] simultaneously we will have to do [3:41] multi-threading and this is done the [3:43] following way we used the threading [3:44] module to define a thread by saying [3:46] threading do thread and then we can [3:49] specify a Target function so Target [3:51] equals and we can pass the worker [3:53] function here as an object passing it as [3:56] an object means not passing it with the [3:57] parenthesis so not calling it but just [4:00] uh passing the instance of worker just [4:03] passing the object worker which is the [4:05] function The Entity worker um and then [4:09] this is the object this is the actual [4:10] threat we can either store it in a [4:12] variable and then start the variable or [4:13] we can start it right away like this by [4:16] setting saying threading do thread [4:18] Target equals worker. start and this is [4:20] going to now start the worker function [4:22] in a separate thread so if I run this um [4:25] you will be able to see press enter to [4:27] quit 1 2 3 I'm not pressing anything if [4:29] I now press enter it goes past the [4:32] statement and it sets done to true and [4:34] since done is set to true this also [4:36] terminates this worker now this would [4:39] not work of course if I have here an [4:41] endless loop while true then even if I [4:44] press enter it's not going to quit [4:46] because then uh the main thread doesn't [4:48] have anything to do but this threat is [4:50] still running however we can also change [4:53] that by saying that this threat is not [4:55] an important threat we don't want to [4:57] keep the program running if everything [4:59] else has finished finished this is a [5:00] so-called Damon threat which basically [5:02] means if nothing else is running you can [5:04] also quit the script even though this [5:06] threat is running we can specify here [5:08] Damon equals true and this basically [5:11] means what I just said that this is [5:12] running in the background and as soon as [5:15] the main threat and the other important [5:16] threats have finished we can also quit [5:19] the script we don't have to wait for [5:20] this particular threat here because it's [5:23] just a Damon threat so I can run this [5:24] now and I can press enter And even [5:27] though this enter doesn't change [5:29] anything about this Loop even though [5:30] this threat is still running we're just [5:32] terminating the whole script because [5:33] this is just a Damon threat that is what [5:36] this is for we can also pass arguments [5:38] by just passing text here for example [5:41] and saying that we want to print an F [5:44] string with text and then the counter [5:47] just making up some stuff here uh we can [5:50] do that by passing arguments so we can [5:52] say arcs equals and we can pass a tupal [5:55] of arguments here and in this case we're [5:56] going to just pass ABC but since this is [5:58] a tupal we need need to also pass a [6:00] comma otherwise this is just going to be [6:02] treated as a single value but we need a [6:04] tuple uh we can do the same thing we can [6:06] run another thread with the same [6:08] function XYZ here as a text and you're [6:11] going to see what this actually does you [6:14] can see that it uh prints ABC counter oh [6:17] actually I just noticed that we're uh [6:19] printing counter we need of course to [6:21] print the actual value of counter you [6:25] can see here now abc1 xyz1 uh how these [6:28] are running in parallel and if I press [6:30] enter since both of them are Damon [6:32] threats we are quitting the script if I [6:34] set now one of them not to a Damon [6:36] threat and the other one uh to a Damon [6:38] threat uh you will see that if I just [6:40] press enter now one of them is going to [6:42] stop um [6:45] actually doesn't stop any of those oh of [6:48] course sorry this was uh this was not a [6:50] correct thought because since one of [6:52] them is running the other one is going [6:53] to run as well because if we set one of [6:56] them to not a Damon thread this is still [6:58] an important thread and because the [7:00] script is not finished because there is [7:02] some threat still running the Damon [7:03] threat is going to run as well um so [7:06] setting one of them to false to not [7:09] being a Damon threat keeps all of them [7:10] running because Damon doesn't mean that [7:12] you're just quitting it but it just [7:14] means that since something is still [7:16] running we're going to keep this running [7:18] as well but if nothing else is running [7:19] we're going to uh not keep this running [7:22] anymore and the last thing I want to [7:24] show you here I'm not going to show you [7:25] here as a demonstration but just as a [7:27] concept is if you have a list of mult [7:29] multiple threats if you say for example [7:31] this is T1 and this is T2 and you don't [7:34] call the start but you just save them as [7:36] objects here and then you do T1 start [7:39] and T2 start this is the same [7:41] functionality here you can also wait for [7:43] those threats so if you do something and [7:45] you don't want to continue with this [7:47] here until both threats have finished [7:49] with the execution then you can say t1. [7:52] jooin and t2. jooin and you would not [7:55] get to this code section here um until [7:58] the threats have finished so you can see [7:59] it doesn't even print press enter to [8:01] quit because it doesn't get to that line [8:03] since both threats are still running so [8:06] that's it for today's video I hope you [8:07] enjoyed it and hope you learned [8:09] something if so let me know by hitting a [8:10] like button and leaving a comment in the [8:12] comment section down below and of course [8:13] don't forget to subscribe to this [8:14] Channel and hit the notification Bell to [8:16] not miss a single future video for free [8:18] other than that thank you much for [8:19] watching see you in the next video and [8:21] bye [8:35] [Music]