This is over 3 years old, and it's in Py2. And yet, one of the best Python threading tutorials ever. Well done, my good man. Very helpful, and very well explained.
Honestly to me I think he doesnt answer any questions in the comments because the code and slides are from the book called Violent Python, even the powerPoint are from Violent Python. he does understand python but the fact that he doesnt explain why he adds each line of code..... for his tone of voice he is reading the code from the book.... I just came here to look some original content about nmap scan in the book Violent Python, because is not about just reading the book, but understanding and he doesnt really explain anything...
Very well done. Logical, not too much code, not too little, only exactly as much as is needed to demonstrate the concepts. I wanted to learn python threading in 30 minutes, and your video helped me do just that. Thanks!
Thanks a lot man! I was able to use what I learned here in multiple projects. One of which involved a game timer running in the background, counting down until game over! Well done explaining! :D
Totally agree with nuntius. I watched countless videos and read tutorials trying to learn to use threading. One watch of your video and it clicked. Thanks.
I just wanted to let you know that I've had a really hard time deciphering how to use threading, but this video finally helped me figure it out. Thanks! My program is now at last working.
Hey Draps! This video just solved my GUI problems. I spent months not knowing what was wrong with it. Halfway thru this video and I already knew how to fix it. Thank you, sir...*subscribed*
Thanks for the great job. I'm looking to learn python now for a future job in linux sysadmin that will also imply pyscripting. Even tho I have only a basic understanding of the programming languages I really do understand your explanations and again I really appreciate the effort you put into making those videos
+Corey Vollmer Thanks! Great work! There is a python3 version of this series! So be sure to check it out if you want to look at more videos in this series.
Really nice tutorials. You have covered advanced python topics in simple and precise manner. Some tutorials on python setuptools and decorators will be really helpful...getting a bit greedy yeah ;)
Hi Its an awesome video, job well done :) It would be nice if you could come with examples using semaphores,condition and event based locking as well. Hope to see it sooner some time.
Dude thanks for this tutorial! This is what I exactly wanted. I noticed steam icon on the taskbar, what games do you play or can I share your username?
I have learnt a lot through your videos. thanks. I have a problem. I have 3 computer nodes (A, B, C). I want to send a file from A to C via B. Whatever packets node B is getting from A should get transferred directly to node C (if C is connected to B). I have been told that my problem could be solved using synchronous threading but I do not know how. Please help me out.
Shivam Garg Threading may be useful. Are you familiar with Python Networking? Well B must know about A & C so you should be able to open two sockets, one connecting to A and one connecting to C. Then just pass the information along: tempData = socketA.recv(2048) #receive from A #maybe store data?, count it? socketC.send(tempData) #send on to C Using threading, you may have a socket open in each thread. and pass the data between the threads one sending and another receiving. If you take this approach, using a queue for storing the data would be a wise move to avoid thread access issue.
can you please explain multi threading and multi processing using python program, taing an example with sorting and removing duplicates from bigger files?
A little tipp for your terminal work in ubuntu or every bash shell. If you add in the first line of your file "#! /usr/bin/python" your systems knows wich interpreter or compiler it has to use, important if you have to use different versions etc. After this you can make your file exectutable with "chmod +x file". After this you can execute the file with "/file". This is not necessary for your kind of tutorials but "if __name__ == "__main__:" is also not realy necessary in your short programs ;)
Sir when we use locks our program will become slow..as we can only use thread at a time then what's the point of using threads when we are executing just a single thread in our program
What is the use of background.join()?When I place it after background.start() it's first writing into the file and then printing 200.If I place it after print it's first printing and then writing into the the file.Can you tell me what is the difference?what is the difference between start and run in the above code?
while we were using locks to print timer1 and timer2, shouldn't the order of the printed statements be: Timer: Timer1 has started timer1 has acquired lock then printing the 5 line consisting of time timer1 has released lock Timer: timer2 has started timer2 has acquired lock then printing the 5 line consisting of time timer2 has released lock main is completed i was expecting this output bcz once the thread t1was printed, the statement "timer: timer2 has started" Plz explain
great; thank you. at 22:24 timer-1 started at 17:41:56 and about the same time acquired the lock, then quickly afterwards Main completed and timer-2 started at about the same time. 41:56, first 5 times output from timer-1 are :57 58 59 60 01 so does that mean all this while timer-2 waiting for lock to be released? then timer-2 started to output at :03 05 07 09 11. I am confused if anything being processed in parallel. I am beginner, hopefully I will get is some day. Your tutorials have been quite helpful.
Hi, as I can see from the code you typed in for acquiring lock and releasing them just to make sure that print function in python should be accessible by only one thread, you were acquiring lonk after printing one line in your timer() function. Don't you think acquiring lock before printing first line in timer() function would be an ideal way?
Yeah, you really only want one thread managing your output. With the last example I was trying to make it as obvious as possible for people to see how locks work. You will see that both threads start, but one obtains the lock, then the other thread has to wait.
if I may ask, in this video, what is the purpose of the lock? you can as well just call timer two times without the lock. Are there other applications?
The lock in this video is just an example of what a Lock does. If you ever have an issue where two threads are interacting with shared data and issues are arising because both threads are writing at the same time or reading out of sync. Then locks are going to help you out.
It makes no sense to me saying that if I have 10 threads my processing will be 10 times faster. Doesn't it depend on if the process is intensive or not and on the number of CPUs I have? To me, If I have only one CPU and my process takes 10s to run, it will still get 20s to run the same code twice with 2 threads, but they will finish at the same time.
+Sami Kanafani It's a little unpredictable to do that. it's also platform independent. generally you want to make sure a program has closed and finished with the file before your open it again to read or write.
When I try the simple timer example I don't get timer2 starting until timer1 finishes. The two threads don't seem to be running in parallel but sequentially. Is there something I'm missing? Is there some way I need to enable threading on my machine? I've tried using Ubuntu and CentOS and get the same result (both have multi-core cpus)
Did you use the example 3 code? Because there we used Threading *Locks* to lock onto timer1 so that timer2 has to wait for timer1 has to finish (To easily teach what locks are doing). Locks are used to wait for data to be safe to access. Example 1 and Example 2 should provide threaded processing.
Thanks for the reply. I figured out the problem. Example one was giving me syntax errors so I "fixed" them so the script would run. In doing so I called Thread incorrectly. After some Googling I figued out the error and I got it to work as intended. thanks again.
James The Thread Base class calls run() when start() is called. so when we create our own subclass we get all of the base class methods. we just override what the run method does.
the asyncWrite class is a thread, it inherits from the thread base class. So instead of using the threading.Thread generic class we create a custom class that has a specific purpose.
My program doesn't end when I run the first code example. Anyone know what the problem might be? I'm using Python 2.5 on Mac with the following code: from threading import Thread import time def timer(name, delay, repeat): print "Timer: " + name + " started" # Run for amount of times given in repeat argument while repeat > 0: # Sleep for amount stated in delay argument time.sleep(delay) print name + ": " + str(time.ctime(time.time())) repeat -= 1 print "Timer " + name + " completed" def Main(): # Thread 1 # function #func arguments ### Creates Thread object t1 = Thread(target = timer, args = ("Timer 1", 1, 5)) t2 = Thread(target = timer, args = ("Timer 2", 2, 5)) # Method that starts the thread t1.start() t2.start() # Will show when Main function completes print "Main Complete" if __name__ == '__main__': Main()
+DrapsTV Hey, before it was just giving me a cursor instead of going to the next shell line. I had to use keyboard interrupt to stop the program. But I ended up running the program from my bash terminal and it worked fine. Apparently it's an IDLE thing. I should really stop using IDLE...
Semaphores are the same as locks however you can specify the number of threads that can access data before it's locked until another thread releases one. if I had three threads and a semaphore with a value of two, the third thread to try access the data would have to wait until thread 1 or 2 is finished with it first.
Best tutorial on youtube
All your python advanced tutorials are saving my life.
This is over 3 years old, and it's in Py2.
And yet, one of the best Python threading tutorials ever. Well done, my good man. Very helpful, and very well explained.
Honestly to me I think he doesnt answer any questions in the comments because the code and slides are from the book called Violent Python, even the powerPoint are from Violent Python. he does understand python but the fact that he doesnt explain why he adds each line of code..... for his tone of voice he is reading the code from the book.... I just came here to look some original content about nmap scan in the book Violent Python, because is not about just reading the book, but understanding and he doesnt really explain anything...
Very well done. Logical, not too much code, not too little, only exactly as much as is needed to demonstrate the concepts. I wanted to learn python threading in 30 minutes, and your video helped me do just that. Thanks!
+Nick Shaver That's awesome! I'm glad you enjoyed the video! :) Thanks for leaving such a nice comment!
Thanks a lot man! I was able to use what I learned here in multiple projects. One of which involved a game timer running in the background, counting down until game over! Well done explaining! :D
CFilm Thanks for your support! glad you found it useful!
Totally agree with nuntius. I watched countless videos and read tutorials trying to learn to use threading. One watch of your video and it clicked. Thanks.
Keith Klassen Glad you found it useful! I had the same experience with threading!
THANK YOU! i read so many tutorials online but yours is the only one that i actually understood!
Something Original Awesome! :)
I would love to see more python threading tutorials! more advance stuff!
Thanks a bunch my dude! Got into Python recently from other languages, your video was a great explanation!
I just wanted to let you know that I've had a really hard time deciphering how to use threading, but this video finally helped me figure it out. Thanks! My program is now at last working.
nuntius91 That's awesome mate! I had a similar problem when I was learning them too. I'm glad I was able to help!
Thank you very much, I had a difficult time learning how to multithread. But found your tutorial very helpful!
+Evan Young That's awesome mate! Thanks for letting me know!
Good tutorial on threading.now i can see locks unlocked in my head
Hey Draps! This video just solved my GUI problems. I spent months not knowing what was wrong with it. Halfway thru this video and I already knew how to fix it. Thank you, sir...*subscribed*
Sooooo helpful! This is EXACTLY what i needed! Thank you so much, i wish you a great day!
Man, your tutorial was amazing!!!
Thanks for the kind words!
Excellent tutorial, many thanks.
Thanks for the great job. I'm looking to learn python now for a future job in linux sysadmin that will also imply pyscripting. Even tho I have only a basic understanding of the programming languages I really do understand your explanations and again I really appreciate the effort you put into making those videos
Excellent explanation.
thanks a lot for making this video, you just cleared all the confusion I ever had. Cheers!!!!!
Locking demo was easy to understand...thx
Pretty helpful, thanks! I had a few snags using Python 3 but was fairly easy to resolve.
+Corey Vollmer Thanks! Great work! There is a python3 version of this series! So be sure to check it out if you want to look at more videos in this series.
+DrapsTV Did not know that, will check it out thanks.
Thank you for a clear explanation. Helped me a lot:)
very good info on threading
we would like more on threading vs multiprocessing
really nice tutorial !
Big Thanks! )))
Waiting for Networking lesson )))
Really nice tutorials. You have covered advanced python topics in simple and precise manner.
Some tutorials on python setuptools and decorators will be really helpful...getting a bit greedy yeah ;)
I'm glad you like them. Hopefully one day I'll get around to writing a tutorial on setuptools :)
Good intro and tutorial.
Thanks.
Hi Its an awesome video, job well done :) It would be nice if you could come with examples using semaphores,condition and event based locking as well. Hope to see it sooner some time.
clearly explation to me, thanks.
Your channel and really fantastic, won a Brazileiro registered and tanned on your video!
Thanks! I'm glad you enjoy it!
excellent review
Thanks bro! This helped
Dude thanks for this tutorial! This is what I exactly wanted. I noticed steam icon on the taskbar, what games do you play or can I share your username?
Thanks a lot, such a clear explaination.
+bibek mantree Thanks mate!
I have learnt a lot through your videos. thanks.
I have a problem.
I have 3 computer nodes (A, B, C). I want to send a file from A to C via B. Whatever packets node B is getting from A should get transferred directly to node C (if C is connected to B). I have been told that my problem could be solved using synchronous threading but I do not know how. Please help me out.
Shivam Garg
Threading may be useful. Are you familiar with Python Networking? Well B must know about A & C so you should be able to open two sockets, one connecting to A and one connecting to C. Then just pass the information along:
tempData = socketA.recv(2048) #receive from A
#maybe store data?, count it?
socketC.send(tempData) #send on to C
Using threading, you may have a socket open in each thread. and pass the data between the threads one sending and another receiving. If you take this approach, using a queue for storing the data would be a wise move to avoid thread access issue.
can you please explain multi threading and multi processing using python program, taing an example with sorting and removing duplicates from bigger files?
Hi Draps TV, hoe can watch your remaining advanced training videos, i cant find it on your channel
A little tipp for your terminal work in ubuntu or every bash shell. If you add in the first line of your file "#! /usr/bin/python" your systems knows wich interpreter or compiler it has to use, important if you have to use different versions etc. After this you can make your file exectutable with "chmod +x file". After this you can execute the file with "/file".
This is not necessary for your kind of tutorials but "if __name__ == "__main__:" is also not realy necessary in your short programs ;)
Awesome Tutos :D
Thank you!
Sir when we use locks our program will become slow..as we can only use thread at a time then what's the point of using threads when we are executing just a single thread in our program
What is the use of background.join()?When I place it after background.start() it's first writing into the file and then printing 200.If I place it after print it's first printing and then writing into the the file.Can you tell me what is the difference?what is the difference between start and run in the above code?
It is awesome!I want to learn more knowledges about this topic.Thanks for your share. :)
Great tutorial. One suggestion: You can just use print name + ": " + str(time.ctime()) instead of print name + ": " + str(time.ctime(time.time()))
while we were using locks to print timer1 and timer2, shouldn't the order of the printed statements be:
Timer: Timer1 has started
timer1 has acquired lock
then printing the 5 line consisting of time
timer1 has released lock
Timer: timer2 has started
timer2 has acquired lock
then printing the 5 line consisting of time
timer2 has released lock
main is completed
i was expecting this output bcz once the thread t1was printed, the statement "timer: timer2 has started"
Plz explain
great; thank you. at 22:24 timer-1 started at 17:41:56 and about the same time acquired the lock, then quickly afterwards Main completed and timer-2 started at about the same time. 41:56, first 5 times output from timer-1 are :57 58 59 60 01 so does that mean all this while timer-2 waiting for lock to be released? then timer-2 started to output at :03 05 07 09 11. I am confused if anything being processed in parallel. I am beginner, hopefully I will get is some day. Your tutorials have been quite helpful.
Hi, as I can see from the code you typed in for acquiring lock and releasing them just to make sure that print function in python should be accessible by only one thread, you were acquiring lonk after printing one line in your timer() function. Don't you think acquiring lock before printing first line in timer() function would be an ideal way?
Yeah, you really only want one thread managing your output. With the last example I was trying to make it as obvious as possible for people to see how locks work. You will see that both threads start, but one obtains the lock, then the other thread has to wait.
if I may ask,
in this video, what is the purpose of the lock?
you can as well just call timer two times without the lock.
Are there other applications?
The lock in this video is just an example of what a Lock does. If you ever have an issue where two threads are interacting with shared data and issues are arising because both threads are writing at the same time or reading out of sync. Then locks are going to help you out.
Thanks! :D
Thanks, very helpfull video. Will try it on a raspberry pi.
You Are Awesome Man ! can you make videos about the basics of python?
I do have a beginner series that can get you up and running, There is also an Intermediate series too :)
Python Beginner Tutorial 1 - Intro & Setup
Thanks
perfect! thanks!
It makes no sense to me saying that if I have 10 threads my processing will be 10 times faster. Doesn't it depend on if the process is intensive or not and on the number of CPUs I have? To me, If I have only one CPU and my process takes 10s to run, it will still get 20s to run the same code twice with 2 threads, but they will finish at the same time.
I have a question:
Can I use Async MultiThreading to write in a file and read from this file while we are still writing in it
Thank you!
+Sami Kanafani It's a little unpredictable to do that. it's also platform independent. generally you want to make sure a program has closed and finished with the file before your open it again to read or write.
+DrapsTV Thank you
Thanks for the tutorials!
Are you from England?
+matteo7moh You're welcome! Nah, I'm from Australia!
When I try the simple timer example I don't get timer2 starting until timer1 finishes. The two threads don't seem to be running in parallel but sequentially. Is there something I'm missing? Is there some way I need to enable threading on my machine? I've tried using Ubuntu and CentOS and get the same result (both have multi-core cpus)
Did you use the example 3 code? Because there we used Threading *Locks* to lock onto timer1 so that timer2 has to wait for timer1 has to finish (To easily teach what locks are doing). Locks are used to wait for data to be safe to access. Example 1 and Example 2 should provide threaded processing.
Thanks for the reply. I figured out the problem. Example one was giving me syntax errors so I "fixed" them so the script would run. In doing so I called Thread incorrectly. After some Googling I figued out the error and I got it to work as intended. thanks again.
So basically the lock is making the thread act as a sequential execution, then why use thread !?
in your second tut how did the thread know its target was self.run()?
James The Thread Base class calls run() when start() is called. so when we create our own subclass we get all of the base class methods. we just override what the run method does.
I hope you will show how to work with sockets ))
Thanks! ))
Yep That's the next video :), Hoping to have it out next week, busy with Easter stuff at the moment :)
Thanks! )))))
Thank you
why tLock is not imported into function as global?
How does the 'run' function in the asyncWrite class runs without specifying the 'target' while creating the thread?
the asyncWrite class is a thread, it inherits from the thread base class. So instead of using the threading.Thread generic class we create a custom class that has a specific purpose.
I got it. Thanks for such simple explanatory video.
Thank you for this tutorial, I got my stuff working, having the GUI freeze up when the program is busy is pretty...... ehh....
That's awesome to hear! Yeah freezing GUI isn't very cool...
Liked, commented and subscribed (y)
amazing one .. thanks for share ... :)
seems really similar to creating instantiating a new object. Am i right?
+moog500 didn't finish the video; I am stupid
+moog500 You are far from stupid mate!
can you tell me how to learn networks from scratch
i find scary that your seconds are equal to my seconds in the timer lol
*X-Files soundtrack*
My program doesn't end when I run the first code example. Anyone know what the problem might be?
I'm using Python 2.5 on Mac with the following code:
from threading import Thread
import time
def timer(name, delay, repeat):
print "Timer: " + name + " started"
# Run for amount of times given in repeat argument
while repeat > 0:
# Sleep for amount stated in delay argument
time.sleep(delay)
print name + ": " + str(time.ctime(time.time()))
repeat -= 1
print "Timer " + name + " completed"
def Main():
# Thread 1 # function #func arguments ### Creates Thread object
t1 = Thread(target = timer, args = ("Timer 1", 1, 5))
t2 = Thread(target = timer, args = ("Timer 2", 2, 5))
# Method that starts the thread
t1.start()
t2.start()
# Will show when Main function completes
print "Main Complete"
if __name__ == '__main__':
Main()
+James McCoy How did you go mate? Did you figure out what was wrong? What kind of output where you getting?
+DrapsTV Hey, before it was just giving me a cursor instead of going to the next shell line. I had to use keyboard interrupt to stop the program. But I ended up running the program from my bash terminal and it worked fine. Apparently it's an IDLE thing. I should really stop using IDLE...
and 2.5 !
I ran into the same issue with IDLE (3.5). I'm guessing it's the IDLE interpreter thread that is getting hung.
Now it's been so long I forget how I fixed it...
show an example on semaphore too please..:)
Semaphores are the same as locks however you can specify the number of threads that can access data before it's locked until another thread releases one. if I had three threads and a semaphore with a value of two, the third thread to try access the data would have to wait until thread 1 or 2 is finished with it first.
In the 2nd program can we do background=Thread(target=AysncWrite,args=(message,'out.txt') instead
thanks :D
why d not use windows python ??
you shouldn't have used the time module in the example
damn tabs.. but video is good :)
Vim? Seriously? Dude, use PyCharm or VS with PTVS...
Multithreading is done by concurency. Nothing is parralel here..
sjeez use .format
Hoever ben je met python, ik kan met ableton live wel hulp gebruiken. Het scripten voor mijn apc40 mpoet in python namelijk.
I haven't done any complex formatting here so I wouldn't say it was necessary.
vrij ver
format is always necessary :P
format is not always necessary. It's slow. I would never use it when something needs to be fast. :P