- Coding Videos/
- Python /
- Learn to Program 14 : Python Threads
Learn to Program 14 : Python Threads

Download Video link >
Learn to Program 14 : Python Threads
Code & Transcript : http://goo.gl/vnwUy4
Best Python Book : http://amzn.to/2aapV6S
Support me on Patreon : https://www.patreon.com/derekbanas
In this part of my learn to program series with Python we’ll focus 100% on threads. We’ll learn about sleep(), strftime(), the Threading Module, Creating Threads, activeCount(), enumerate(), Subclassing Threads, run(), start(), is_alive(), getName(), setName(), join(), Synchronizing Threads, acquire(), release(), Lock() and more.
If you missed any of the previous videos, the series starts here : https://www.youtube.com/playlist?list=PLGLfVvz_LVvTn3cK5e6LjhgGiSeVlIRwt
Thank you to Patreon supports like the following for helping me make this video
https://www.facebook.com/cottageindustriesbuild/
@kyleaisho
@thetwistedhat
vjFaLk
source
View Comments source >
Transcript view all >
00:00 well hello internet and welcome to part
00:01 14 of my learn to program series in this
00:04 part of the tutorial I'm going to focus
00:05 100% on threads if you haven't seen any
00:08 the previous parts of the tutorial I
00:10 provide a link here in the video so you
00:12 should watch those otherwise you might
00:14 be confused also underneath the video in
00:16 the description you will find a link to
00:18 a transcript as well as all the code you
00:20 find here in this program and I have a
00:22 lot to do so let's get into it okay so
00:25 here we are now whenever you use threads
00:28 is like you're running multiple programs
00:30 at once threads actually take turns
00:33 executing however however it might not
00:35 seem like that and while one is going to
00:37 execute the other is going to sleep
00:39 until it's its turn to execute and a
00:42 thread is just a block of code that
00:44 executes that's it it's we've been
00:47 working with one thread the whole entire
00:49 time now we're going to have multiple
00:50 threads so let's come in here and let's
00:52 just import what we need so we need to
00:55 import threading and I'm going to do
00:57 something with time so I'm an import
00:59 time and I'm going to import a random
01:02 now let's come in here and we will
01:04 define a function that will execute as a
01:08 thread and it's just going to receive a
01:11 single value here and what I'm going to
01:13 do here is print out some information on
01:17 the time so I'm going to say print and
01:19 I'll say thread sleeps at and I'm going
01:25 to print out the time when it goes to
01:26 sleep and then we'll use format to put
01:29 the information inside of here and we'll
01:31 just have eyes going to represent an
01:33 identification for the thread that we're
01:36 going to be executing here and then I'm
01:38 going to use a formatted time method
01:42 from the time import we have here so
01:44 it's STR f t I M so it's formatted time
01:49 so this is something new to see and
01:51 inside of here I'm going to define
01:53 exactly what I want to say so I want to
01:55 see the hour with a colon and then I
01:58 want to see the minutes with a colon and
02:01 then I want to see the seconds with a
02:04 colon and then after the
02:06 at the time and I will get gymtyme then
02:10 I want to generate a random sleep period
02:14 for my threads so I'm going to come in
02:16 and generate a random integer and it's
02:19 going to be between 1 and 5 seconds just
02:22 so we have something a little bit
02:23 different here and let's name this sleep
02:25 so that that's spelled right and then to
02:27 make a thread or a group of code pause
02:31 and let other threads jump in and
02:33 execute we call time and sleep and then
02:36 inside your I'll put the random sleep
02:38 time and then after that I'm going to
02:41 come in and print out after the thread
02:43 it has awoken
02:45 so we'll say thread stops sleeping at
02:50 any specific time here and then we'll
02:53 come in and do the format again so we
02:55 can just copy this whole entire thing
02:57 let's just copy that and paste that
02:59 inside of there and then down here oops
03:02 actually I see a common error here
03:03 you're going to zoom in on here this is
03:05 a question I've get a lot it says end of
03:08 statement expected whenever you see that
03:10 normally just means you have one too
03:12 many parentheses so there you go solve
03:14 that little error now we can come down
03:16 here and start creating some threads and
03:18 then run them and see what happens so
03:20 I'll say range 10 and then I'm going to
03:23 create a thread and to do so you just go
03:27 threading and thread and then you have
03:30 to tell it what you want to have
03:32 executed so the name of that function is
03:35 going to be equal to and this has to be
03:38 called target the name of this is going
03:40 to be execute thread however we get rid
03:42 of those parentheses and then you have
03:44 to pass arguments into it so these are
03:47 things that are required and this is
03:50 expecting a sequence to be passed to it
03:52 so what we're going to do is we only
03:54 want to pass in an identification for
03:57 the thread name so we're going to put a
03:59 comma after that and what's going to
04:01 happen here is each time we cycle
04:03 through our looping structure here a
04:05 thread object is going to be created and
04:08 then we're going to pass it to the
04:10 method that needs to be executed or the
04:13 function it needs to be executed as well
04:14 as the arguments that need pass to that
04:17 specific function
04:18 and then after we do that we say thread
04:20 and to start the execution we call start
04:23 and then I'm going to print out some
04:24 additional information and we say active
04:27 threads and what this is going to do is
04:30 print out the number of threads that are
04:32 currently executing and we could also
04:34 come in here and return a list of all
04:36 the active thread objects that are out
04:38 there so we could say thread objects and
04:42 to get those we just call threading once
04:45 again and enumerated with the
04:47 parentheses inside of there all right so
04:49 we're going to create 10 threads and
04:52 execute them and don't forget to come up
04:53 here and put in inside of there alright
04:56 so now we got that set let's run it up
04:58 got a little bit of a bug here see what
05:00 this is ah so this right here says we
05:02 need to call this a numerate
05:04 unenumerated sorry about that and let's
05:07 run it and you can see there we go and
05:09 we are running all right so what's going
05:11 on here so we can come up to the top and
05:13 you can see that thread 0 is going to
05:15 start however inside of the code it's
05:19 going to come in here and then execute
05:21 this statement right here and then it's
05:23 going to go to sleep whenever it goes to
05:25 sleep that means that it's going to be
05:28 able to call for the second thread to
05:30 execute now you may ask yourself right
05:32 here why are there 2 active threads well
05:35 you can see right here the answer is
05:37 main thread the actual program this guy
05:39 right here is also a thread so that's
05:43 the reason why so you have this up here
05:45 that's going to be running this code up
05:47 here with the first thread you create as
05:49 well as this is also going to be a
05:51 thread so that's the reason why you have
05:53 to there and you can see we were able to
05:55 print out the information in regards to
05:57 what threads were executing and you can
05:59 see each time that it's going to be
06:01 incrementing upwards and you can also
06:04 see the number of threads is also going
06:05 to be incrementing upwards now we came
06:07 inside of here and we said we wanted it
06:09 to go to sleep which means we want the
06:12 next thread in line to be able to
06:14 execute and this is going to be a random
06:15 value between 1 and 5 and if you come
06:18 down here you're going to see thread 3
06:20 stop sleeping at and the specific time
06:23 and the reason why they're out of order
06:24 is because they are going to sleep for
06:27 different periods of time between 1 and
06:29 5 seconds
06:30 and one in five seconds is like a
06:31 million years in computer world and
06:33 that's the reason why you don't have
06:35 these stumbling all over each other and
06:36 also the reason why you have thread
06:38 three stop sleeping right here before
06:40 the Red 9 + 8 + 0 + 2 + 4 + na da da da
06:44 da so there you go
06:45 very simple example of how threads work
06:49 and it's just basically they're going to
06:51 line up and create a whole bunch of
06:53 threads that want to execute the same
06:55 exact code doesn't have to be the same
06:57 exact code by no means but then you're
06:59 going to take turns each time one of the
07:01 threads goes to sleep executing their
07:04 specific lines of code until all the
07:06 threads have executed their lines of
07:08 code and everybody's happy and the
07:10 program finishes and now I'm going to
07:12 get into a little bit more complicated
07:13 topic I'm going to talk about how we can
07:15 subclass a thread and basically you can
07:18 subclass the thread object and then
07:20 define what happens each time a thread
07:23 is going to be executed inside of a
07:25 method called run so we're going to
07:27 create this just like we create all of
07:29 our classes so I am going to call this
07:32 custom thread and I'm going to define
07:35 that I want threading - I want this to
07:38 subclass the threading thread object
07:41 that's available inside of here now I
07:43 have to just define everything so I'm
07:45 going to say Oh
07:46 initialize and it's just going to
07:49 receive name so that we can identify the
07:52 different threads inside of here and
07:54 then what you have to do is call for the
07:56 super thread class to execute and I'm
08:00 just going to call initial eyes on that
08:02 guy and then you just pass self inside
08:04 of there and that's all you need to
08:05 worry about with that and then
08:07 afterwards I'm going to come in here and
08:09 store the name that was passed inside of
08:11 here so we can refer to that once again
08:13 we're going to come in and we're going
08:15 to define the run method and this is
08:18 going to be the method that's going to
08:19 execute whenever we call for our thread
08:22 object to execute and basically what
08:24 it's going to do is it's going to call a
08:27 function get time and to it it is going
08:30 to pass the name so that we can know
08:32 what thread is working and then
08:35 after that we were going to go Prince
08:37 and we'll say thread and then get the
08:40 threads name and then we'll say
08:42 execution ends so this time what we're
08:46 doing is we're having all the code
08:47 executing an outside function and then
08:50 jump out inside of here jump back inside
08:52 of here and that is what's going on so
08:55 another thing we can do now is to create
08:58 or get time function once again it's
09:01 going to receive the thread name and
09:04 inside of this we're going to print out
09:06 exactly the same thing we did before
09:08 which is this guy which is going to
09:10 print out the threads name and also
09:12 whenever that thread went to sleep to
09:14 allow other threads to execute and then
09:17 we'll generate our random sleep time in
09:20 exactly the same way so you're going to
09:22 see the same exact example just done in
09:24 two different ways one using a thread
09:27 object and another one using a custom
09:29 thread object so random int and then
09:32 we'll do 1 through 5 just like before
09:35 and then inside of here also we're going
09:38 to be able to code and call for sleep to
09:40 occur for a thread and random sleep time
09:43 is that guy then we have to do things a
09:45 little bit different we're going to
09:47 create some thread objects this time so
09:49 we're going to say thread 1 is going to
09:51 be equal to and then we have to call our
09:54 custom thread and all at once passed is
09:57 a name and then let's go and create two
10:00 threads here paste that side there let's
10:02 call this thread two and give it the
10:05 name of two and then to start the
10:07 execution you're going to do exactly the
10:09 same thing you did previously you're
10:11 just going to call start on this and
10:13 thread two and start stop starts
10:17 executing we can come in here and check
10:20 if a thread is alive which means that
10:22 it's running here and to do so we just
10:25 reference the threads name followed by
10:28 is alive and that's going to come back
10:30 with either true or false and it will do
10:32 exactly the same thing with thread - we
10:35 could also get our thread name so let's
10:38 say print and thread one name and that's
10:42 going to come back
10:43 one as you'll see throw one and get name
10:46 and you're going to be able to also
10:48 change the name or set it with set name
10:51 but we're going to get it this time so
10:53 we don't want to put the set name inside
10:55 of there obviously I'm going to do the
10:57 same thing with thread two and two and
11:00 then we use join to come in here so that
11:05 we can wait for threads to exit and then
11:08 we'll do the same thing with the next
11:11 one and then finally we come in here and
11:13 just print a little message that says
11:16 execution and so we know what's going on
11:19 so let's run this and you can see right
11:21 here pretty simple thread one is going
11:24 to call the get time method which is
11:26 going to say that thread one sleeps at a
11:29 specific time and like I said a second
11:31 in the computer world is extremely
11:34 extremely long so that's the reason why
11:36 these are exactly the same time they go
11:38 to sleep at exactly the same time you
11:40 could see that we came down here and
11:42 asked if they were alive and it came
11:43 back as true and true you can see that
11:45 the name comes through as one and two of
11:48 course just like we have here and you
11:50 can also see thread to execution ends
11:52 and thread one execution ends so thread
11:55 should make a lot of sense now so what
11:57 I'm going to do now is jump into a
11:59 real-world situation so we can see an
12:01 example of why we would use threads all
12:05 right so whenever you are using your
12:07 threads you can lock other threads from
12:10 executing so let's say if we tried to
12:12 model a bank account in that situation a
12:15 thread would make a lot of sense or
12:17 using threads would make a lot of sense
12:18 because let's say you only had a hundred
12:21 dollars in the account however there
12:23 were three different people that had
12:24 access to that account now you wouldn't
12:26 want them to go and withdraw more money
12:29 out of the account than is actually
12:30 there so what's going to happen is if
12:33 somebody goes and tries to withdraw
12:35 money from the account all other ATM
12:37 machines or the ability to access that
12:40 account will be locked out until the
12:42 first user is finished withdrawing funds
12:45 from the account so we're going to come
12:47 in here and we're going to create a
12:49 custom class that is going to be a
12:51 subclass of thread that is going to lock
12:53 out other you
12:54 remember using an account so let's come
12:57 in here and let's create that and we're
12:59 going to do the same thing again
13:00 threading and thread going to do the
13:03 same thing we're going to need to
13:05 initialize this guy and in this
13:08 situation I'd like to come in and have
13:11 the name of the person trying to access
13:14 the accounts and the amount of money
13:17 that they are trying to request so we're
13:20 going to come in again and go threading
13:22 and thread and initialize this guy and
13:27 here we'll just pass in self once again
13:29 then we'll also come in and assign the
13:33 name of the person accessing the
13:35 accounts and also the money that is
13:39 going to be requested so that's
13:41 initialized so now inside of run what
13:44 are we going to do we're going to go
13:45 define run first thing we want to do is
13:48 get a lock to keep other threads from
13:50 being able to access our account so to
13:54 do so call thread lock and acquire and
13:57 this guy's actually going to be created
13:59 down in the main thread so I'll go
14:01 thread lock is equal to and I'll go
14:04 threading lock also inside a run I want
14:08 to call a static method that's going to
14:10 go in and try to access and get money
14:13 and withdraw money from the account so
14:15 I'm going to go bank accounts get money
14:17 past self inside of there because I want
14:20 to be able to access the name and the
14:22 money requested inside of the static
14:25 methods so that's a requirement and then
14:27 after it tries to get the money out of
14:29 the account I'm going to release my lock
14:32 to let other threads go in there or
14:34 other customers go in and try to access
14:35 that information now to create a static
14:38 method did this in previous parts of the
14:40 tutorial just go static method like that
14:43 and here is where we're going to allow
14:45 them to access the account and try to
14:46 get their money so we're say get money
14:48 the customer is going to be passed in
14:51 here you can see self so it's going to
14:53 be the bank account for said customer is
14:56 going to be passed inside of here and
14:58 then I want to print out some
14:59 information about exactly what's going
15:01 on inside of here
15:02 so I'm going to say the person's name
15:04 tries to
15:06 with draw and a certain amount of money
15:09 at and then we'll say specific time just
15:13 to have this be a little bit more like a
15:14 real-world thing will go customer name
15:18 is what we want to print out there and
15:20 then we'll say customer and the money
15:23 requested and then we want to come in
15:25 and print out some information about the
15:27 time so there's that same formatted time
15:30 function we saw previously and then
15:32 after this we have to find out if the
15:36 information tactually has money inside
15:38 of it so I'll go bank account and we can
15:41 go account balance and let's come up
15:44 here and we'll throw in a static
15:47 variable that's going to be account
15:48 balance its equal to let's just say the
15:51 person is poor only has $100 and
15:54 subtract out the customers money request
15:58 and see if it is greater than zero and
16:01 if it is we want to come in here and
16:04 update the bank accounts balance by
16:08 subtracting off how much money they just
16:11 requested and then we can print some
16:13 information about the new account
16:15 balance accounts balance so I hope you
16:19 see here a situation in which threads
16:22 really make a lot of sense in a real
16:24 world situation so we can go format and
16:27 then we can go bank accounts and get the
16:31 new account balance otherwise if they
16:34 don't have enough money inside of their
16:37 account we need to print a message that
16:40 says not enough money in account and
16:43 then let's also show them their current
16:45 balance so that they know how much can
16:48 be taken out of it and format that and
16:51 go in and put bank accounts and the
16:55 account balance and then also we can
16:58 commit and go to time sleep and just set
17:01 that to 3 seconds then we'll come down
17:03 into the main part or the main thread
17:06 for our program and make all this work I
17:09 already said this is going to allow us
17:10 to lock our threads from executing let's
17:13 go and
17:13 a couple customers here so bank accounts
17:16 here and Doug is going to be accessing
17:20 it and Doug only once one dollar and
17:22 then we'll do the same sort of thing for
17:25 two more so that you can just see how
17:27 this is going to work so the next person
17:30 is going to be Paul and let's say that
17:33 Paul thought he was going to be sneaky
17:35 and go in there and take all of the
17:37 money we're going to see what happens
17:39 there and then let's say we have Sally
17:41 Sally comes after Paul and tries to
17:44 withdraw money and she wants fifty
17:46 percent or fifty dollars we're then
17:49 going to have to start our threads
17:51 executing and do it in exactly the same
17:55 way as we did previously and there we go
17:58 of course we want the threads to wait
17:59 for the previous threads to terminate so
18:02 we'll do join here and we'll do that for
18:05 all of these guys and then there's Sally
18:08 and then let's print a message here at
18:09 the end so that we know when the
18:12 execution ends all right so now we can
18:15 do this and we can come over here and
18:17 watch it all take place so run it whoops
18:20 little error come over here it says bank
18:21 count has no attribute account balance
18:23 what did I name it come up inside of
18:25 here up I typed out account so let's get
18:27 rid of that and run it now and here you
18:30 can see it's working
18:31 Doug tries withdraw a dollar and that's
18:33 a specific time new account balance is
18:35 $99 Paul tries to withdraw $100 at that
18:38 time it says not enough money in the
18:40 account prints out the current balance
18:42 and then Sally comes along and tries to
18:44 withdraw $50 you can see now the current
18:47 account balance is $49 and execution
18:50 ends so there you go guys there are
18:52 three examples in ways that you can use
18:55 threads hopefully that clears up threads
18:57 because I know a lot of people out there
18:58 we're struggling with understanding how
19:00 they work and if you take some time now
19:02 and think up your own scenarios in which
19:04 threads work write down and go and
19:07 create your own little programs to test
19:09 them out I think there will be no
19:10 problem for you in the future so like
19:12 always please leave your questions and
19:15 comments below otherwise till next time
Leave a Reply