Python Threading Tutorial: Run Code Concurrently Using the Threading Module

  Рет қаралды 822,558

Corey Schafer

Corey Schafer

4 жыл бұрын

In this video, we will be learning how to use threads in Python.
This video is sponsored by Brilliant. Go to brilliant.org/cms to sign up for free. Be one of the first 200 people to sign up with this link and get 20% off your premium subscription.
In this Python Programming video, we will be learning how to run threads concurrently using the threading module. We will also look at how to download multiple high-resolution images online using a ThreadPoolExecutor from the concurrent.futures module. Let's get started...
The code from this video can be found at:
bit.ly/threading-code
List Comprehensions Tutorial - • Python Tutorial: Compr...
Exception Handling Tutorial - • Python Tutorial: Using...
✅ Support My Channel Through Patreon:
/ coreyms
✅ Become a Channel Member:
/ @coreyms
✅ One-Time Contribution Through PayPal:
goo.gl/649HFY
✅ Cryptocurrency Donations:
Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot
✅ Corey's Public Amazon Wishlist
a.co/inIyro1
✅ Equipment I Use and Books I Recommend:
www.amazon.com/shop/coreyschafer
▶️ You Can Find Me On:
My Website - coreyms.com/
My Second Channel - / coreymschafer
Facebook - / coreymschafer
Twitter - / coreymschafer
Instagram - / coreymschafer
#Python

Пікірлер: 715
@coreyms
@coreyms 4 жыл бұрын
Hey everybody! I hope you find this video helpful. I'll be releasing the multiprocessing video next week. I am currently packing up my temporary recording station and will be moving into my new place tomorrow, so I should be able to get it recorded, edited, and released next week for sure. We'll be covering how to use multiprocessing to process the high-resolution images we downloaded in this video. Hope you all get some good use out of these topics!
@youssefelbiyadi4285
@youssefelbiyadi4285 4 жыл бұрын
Hi corey Thank you very much for this topic it's quite interesting, your videos are very helpful. I have a view function in my django project that performs some complicated calculations, so it is CPU Bound. The problem is that when I run these calculations for a high number of objects (each object is an eNodeB site), it takes a while before loading the view page. I tried to use threading but it was not useful. So I wanna use multiprocessing but I have a problem with the __name__=="__main__" condition. I can't use it in Django framework... Do you have any other suggestions for me please that will be very helpful ? Thank you again for all your efforts :)
@coreyms
@coreyms 4 жыл бұрын
@@youssefelbiyadi4285 Hey there. When it comes to running asynchronous tasks on web applications, it might be a good idea to use a messaging task queue (e..g Celery). Anthony over at the PrettyPrinted channel released a video just the other day on how to do this. I think he did a great job. Check that out here: kzfaq.info/get/bejne/mJNmoKhy15fRXXU.html
@youssefelbiyadi4285
@youssefelbiyadi4285 4 жыл бұрын
@@coreyms Tkanks a lot that was very helpful
@gingerboy9170
@gingerboy9170 4 жыл бұрын
Waiting for the next "IMG processing" vid sensei....🔥🔥🔥
@marcelodimb
@marcelodimb 4 жыл бұрын
Hi @Corey Schafer, please, can you make a tutorial explaining how we create a python web application (Django or Flask) using Docker? I love your videos. Best regards from Brazil!
@nicholastalbot6370
@nicholastalbot6370 4 жыл бұрын
Corey: I know that sounds confusing. Me: No, you explained that very well.
@edwingarcia5043
@edwingarcia5043 2 жыл бұрын
Unfortunately some things sound confusing, I mean, it heavy depends on my current knowledge, the good thing about Corey is the calm way of explaining.
@edwingarcia5043
@edwingarcia5043 2 жыл бұрын
@Slime Boi wadafa :0
@HexagonalClosePacked
@HexagonalClosePacked 4 жыл бұрын
When I first tried to switch my career to programming, I watched Corey's videos. Now, 3 years later, I already got a job in programming and still watch Corey's videos :)
@dnetne5508
@dnetne5508 4 жыл бұрын
Damn - that is amazing!
@ksun8993
@ksun8993 3 жыл бұрын
@HexagonalClosePacked So basically you have your rank /Z effective as 6 with 2 corner atoms,3 body and 2 face atoms . And Zn and Mg adopt your structure. (To understand , read your username)
@kwertie133
@kwertie133 3 жыл бұрын
@@ksun8993 iit?
@K3vyB
@K3vyB 3 жыл бұрын
@@kwertie133 I unleashed a vile shit just now bro
@muralidhar40
@muralidhar40 2 жыл бұрын
Same here!
@dipankarkaushik5285
@dipankarkaushik5285 4 жыл бұрын
Was waiting for this topic to learn from the world's best Python Mentor. Now the wait is over. God bless you.
@saraljoshi1
@saraljoshi1 4 жыл бұрын
“hear hear"..exactly my sentiment. After trying concurrency in python on my own several times and failing. I knew only Corey's Videos will help.
@kebman
@kebman 3 жыл бұрын
If you threaded the timer the wait would be much shorter....................
@PyjamasBeforeChrist
@PyjamasBeforeChrist 3 жыл бұрын
time.sleep(0)
@kirubababu7127
@kirubababu7127 2 жыл бұрын
Hi Bro, If I am trying to read CSV files by pandas,time duration is not reducing. Could suggest your ideas?
@CodeWithGBMags
@CodeWithGBMags 2 жыл бұрын
I agree, this was really helpful @Corey, you are my favorite Python mentor and so is your channel
@vishnusingh4118
@vishnusingh4118 4 жыл бұрын
Crystal-friggin'-clear! The coolest part was the real-world examples at the end which ensured there was a concrete takeaway and that it wasn't all just theoretical. The best threading vid on the internet! You keep getting better Corey. More power to you !
@LiquidArremer
@LiquidArremer 4 жыл бұрын
I think the best part of these tutorials are the ones were you involve real world examples to show the utility, especially common cases and how it alters from then to now. Keep up the good work Corey!
@CwMuller
@CwMuller 4 жыл бұрын
Had a service at work that created html pages with indexes of hundreds of xml reports. It used to read each report one by one. Used what I learned here to refactor the code to process files concurrently, which cut the script run time to a fraction of what it used to run at. Thanks for the fantastic tutorial!
@coreyms
@coreyms 4 жыл бұрын
That's awesome! Good work!
@paulh4654
@paulh4654 4 жыл бұрын
You are not only the master of this subject matter, but also you are a great communicator who can articulate artfully. I appreciate your time and effort to share your gift with all of us. Thank you so much.
@excitedbox5705
@excitedbox5705 4 жыл бұрын
This has to be the best made guide ever. This should exist for every programing language. Usually guides only give you the absolute basics and expect you to figure out the more advanced stuff yourself and that is where most people get stuck. The level of detail in this is perfect without making it overly long.
@gurudevilangovan
@gurudevilangovan 3 жыл бұрын
You definitely do have a gift. It takes a ton of talent to explain concepts like this with so much lucidity. Whenever someone says they want to learn python, I point them right to your videos. Thanks Corey! I'm indebted to your videos.
@anantchopra1663
@anantchopra1663 Жыл бұрын
This video is a life-saver! I have a project due in 3 days that needs me to do a lot of fancy stuff in a multithreaded program, and I had no clue what multithreaded programs are! I'm in a much better position after watching this video. Thank you, Corey!
@dano2169
@dano2169 4 жыл бұрын
Literally so many videos out there on threading and multiprocessing but by far this is the best. Thanks Corey!
@andrewhofmann5453
@andrewhofmann5453 2 жыл бұрын
So I just extended the Thread class into my own class and made something so much more efficient because of this video. Great job!
@pacoa-kdbg9303
@pacoa-kdbg9303 Жыл бұрын
Almost 2 years since you uploaded this, you reduced my exect time from ~90 mins to ~2 mins at max . Awesome videos, will continue watching
@okyigwegbe1977
@okyigwegbe1977 4 жыл бұрын
Corey, you are unbelievably good. My python journey has been solid because of your brilliant and simplistic way of explaining the most complex of things. Thank you very much and God bless you.
@chazyua
@chazyua 4 жыл бұрын
OMG. This is not the first time you read my mind. Literally just wanted to find some threading topics ! Wow ! Thanks
@trevormckellen5613
@trevormckellen5613 2 жыл бұрын
The reason I've learned so much from your videos is real world application. I hit a wall with all the tutorials online until I found this channel. Thank you Corey
@fredpaik1349
@fredpaik1349 4 жыл бұрын
Thank you Corey for ALL your videos! You are an inspiration and a great teacher. Keep up the good work!
@borja4015
@borja4015 3 жыл бұрын
These videos were really well explained. I had been curious for a time about multithreading VS multiprocessing and I feel like I learned a lot from your videos
@DarshitParmar
@DarshitParmar 4 жыл бұрын
You are an angel. Most youtubers teach textbook. Thank you so much for this tutorial!
@shinobi9461
@shinobi9461 4 жыл бұрын
Your tutorials are truly phenomenal. Thank you so much for sharing these videos with everyone, they’re incredible!
@touchpad24
@touchpad24 3 жыл бұрын
Your explanation is so simple yet at the same time covers everything. Thanks so much!
@truongtran99_
@truongtran99_ Ай бұрын
Your explanation about threading was so clear and easy to understand. Thank you for making this video!
@alttablife6965
@alttablife6965 2 жыл бұрын
Easy choice to subscribe. These are helping me learn more in a few days than combing through docs did in a week. For reference, I'm coming primarily from a bash background. Last time I looked at multiprocessing was in a college java class. This just refreshed almost everything I need for my current project.
@mohit4u007
@mohit4u007 4 жыл бұрын
Your threading and multiprocessing videos helped me a lot to solve my problem at work. Thanks a millions. Awesome effort and great wishes to you. Keep going!!!
@georgesd9
@georgesd9 3 жыл бұрын
Thank you Corey, channels like yours is what makes youtube worth visiting IMHO. One thing I needed to add, I tried to use threading on a sending email method in flask to avoid delay in redirecting, I found out that it can't be used out of the box, due to flask app-context, with some research I managed to make it work, by researching more about flask contexts I ended up using flask-script with Manager and Shell classes, using the shell to create a context for the thread. I'm writing this in case someone is trying to do the same.
@mehdibouss4551
@mehdibouss4551 4 жыл бұрын
happy hear from u again,.The Mathplot videos were awesome.i saw also all the other videos.Keep going ,U got the juice of python man ! wating for more about data science...
@soumyazyx
@soumyazyx 4 жыл бұрын
More I see these videos, more do I fall in love with python and Corey. Thank you for your time and effort.Keep spreading the knowledge.
@isaacfink123
@isaacfink123 4 жыл бұрын
I have watched almost all of your python videos, and I've been waiting for something like that for a very long time, Thanks corey
@azemashaik4301
@azemashaik4301 10 ай бұрын
Thank you so much, Corey, for this fantastic tutorial on introducing threading in Python to beginners! Your clear explanations and step-by-step demonstrations have really helped me grasp this concept much better. Your tutorials are always a go-to resource for me, and I appreciate how you make even complex topics so accessible. Your contribution to the Python community is invaluable, catering to both newcomers like me and experienced developers. Keep up the fantastic work, Corey!
@cetilly
@cetilly 3 жыл бұрын
Easily the best presentation on multi-threading and ThreadPoolExecutor that I've seen. Really great job.
@ismersh
@ismersh 4 жыл бұрын
The best video in the internet about multithreading. Clear and step by step detailed explanation about different ways to run multiple threads in your script + Corey explained why the threading is useful, while some websites tend to say multithreading doesn't work in python. We are very lucky that we have Corey explaining these concepts for free in KZfaq, because you won't find such good materials even in paid courses on Udemy or other sites.
@coreyms
@coreyms 4 жыл бұрын
Thanks!
@EdiFerreiranet
@EdiFerreiranet 4 жыл бұрын
10:54 Throw away variable!?! Nice!!! I didn't knew that. Awesome!!! Thank you. You're the best ;)
@newpain01
@newpain01 Жыл бұрын
Completely agree about the basic example tutorials, I often find them useless. Great video, thanks!
@MyYouthUk
@MyYouthUk 3 жыл бұрын
I'm currently only half way through... but this is far more comprehensive than anything else I've found. Easy to understand and detailed at the same time.
@derrick7968
@derrick7968 4 жыл бұрын
Corey your the best, love your simplicity and cool examples. I use python for QA automation . Much appreciation from this side of the globe - Kenya.
@spoonmanchen7631
@spoonmanchen7631 4 жыл бұрын
You never let me down, My Instructor. Always looking forward to your next video.
@orkhanahmadov9963
@orkhanahmadov9963 3 жыл бұрын
This man deserves a golden globe award... I have been watching his videos for the last 3 years. He helped me to master Python.. I would definitely like to recommend this channel to anyone who wants to learn CS.
@gytisbl
@gytisbl Жыл бұрын
Really liked the distinction at 4:27 that its best to use several threads when code is IO bound: -reading/writing disk, waiting for network
@letslearnabout4934
@letslearnabout4934 4 жыл бұрын
Brilliant video as always, Corey. I love that it is not just theory or theory with a few "FooBar" examples, but real world applications.
@jaehochoi8062
@jaehochoi8062 Жыл бұрын
Clear, easy, pinpoint, reliable plus truly helpful explanation ever!
@SlaineCZE
@SlaineCZE 4 жыл бұрын
Brilliant explanation (as always). Thank you, Corey. It would be great if you did the more advanced threading/multiprocessing (GIL etc.) video that you suggested.
@dangidelta
@dangidelta 4 жыл бұрын
Mind Blown : 100% New Concept : Yes Easy Implementation : Yes More Control on Programs from now on : Yes Feel like a New Level Achieved : Definitely Yes
@harrisongropper199
@harrisongropper199 3 жыл бұрын
I agree
@alexchung485
@alexchung485 3 жыл бұрын
You have saved my project! I was looking for the return of a thread process everywhere! Thank you!
@adobongasin214
@adobongasin214 2 жыл бұрын
For those who are starting in Python, Corey is the best in terms of explaining things, I am already a Team Lead but I still go back to his tutorials once in a while hahaha
@yuliiakhalus7339
@yuliiakhalus7339 4 жыл бұрын
Thank you very much for this tutorial! No one can explain all this Python stuff better that you!
@gastonscazzuso4113
@gastonscazzuso4113 3 жыл бұрын
Corey , this is the best explained Threading topic video in KZfaq !.. Awesome.. and many Thanks !
@Sharmapawan98
@Sharmapawan98 4 жыл бұрын
Can we have this man rewrite the official docs of python ? Love from India♥
@BoukariYameogo
@BoukariYameogo 4 жыл бұрын
Corey is really a great teacher
@massiveblackwood
@massiveblackwood 4 жыл бұрын
indeed, idk why they refer a link to the same page and hyperlink it on every word on those page now i'm suffering vertago bcs of that thanks for words!
@edgarlip2
@edgarlip2 4 жыл бұрын
this was awesome cut clean tutorial ! best i saw !!! and for sure i am waiting for the next multi processing upcoming videos (local data \ raise conditions \ locks etc ... ).
@mengzhu8385
@mengzhu8385 3 жыл бұрын
Most clear video on explaining multi-threading in Python!
@arunp07
@arunp07 2 жыл бұрын
Great tutorial on Threading. Thanks, Corey, Explanation is at the perfect pace, not needed to increase or decrease the playback speed.
@athisii_ekhe6167
@athisii_ekhe6167 4 жыл бұрын
Your videos doesn't want me to stop. I'm addicted to it. Cos its the best. Thank you. MGBY.
@stephenrwafa2519
@stephenrwafa2519 3 жыл бұрын
Awesome, simply awesome and had to watch ads all the way to the end in appreciation this level of quality. Thanks
@bratergames9410
@bratergames9410 Жыл бұрын
For five years I was looking for some simple explanation that I could use in my code, to pull my dna, from snpedia...OMG. So simple but very good. Thanks man.
@fergusonsofstirling8130
@fergusonsofstirling8130 3 жыл бұрын
Thank you very much Corey. I couldn't get my head around threading before watching your video. After adapting my code with concurrent.futures.ThreadPoolExecutor() / executor.map I managed to speed up a program that makes API calls - which is now at least 100 times faster!
@jerry5646
@jerry5646 4 жыл бұрын
Hands down. Best python tutorial in youtube
@pavelkonovalov8931
@pavelkonovalov8931 4 жыл бұрын
You're doing a godly job! Plz continue with this series. It also would he amazing if you show how to parallelise pandas dataframe transformations , construct new data frames in a sequential manner ,etc. Thank you once again
@matthewson8917
@matthewson8917 4 жыл бұрын
It's awesome that teacher Corey covered what I've been curious about...:)
@moeinmhzg7827
@moeinmhzg7827 3 жыл бұрын
I don't remember if I saw any better python tutorials. You are the best.
@BoukariYameogo
@BoukariYameogo 4 жыл бұрын
How come some people could dislike your videos? I really don't understand it. You're doing a great job for real. Thank you for your videos
@karansmittal
@karansmittal 3 жыл бұрын
It was a very demanded video, Thanks for working on it. Always creating high quality video content.
@terryhenyo9216
@terryhenyo9216 4 жыл бұрын
When I see a Corey Schafer video I automatically like before watching, because I know it's going to be good :)
@derrick7968
@derrick7968 4 жыл бұрын
Me too, i commented without watching
@hotcrossbun26
@hotcrossbun26 Жыл бұрын
I'd be so screwed without your videos. The BEST explanations anywhere on the internet for learning python.
@johnnymysto
@johnnymysto 3 жыл бұрын
Great explanation!! I knew basically nothing about this before I watched it, and it makes complete sense now.
@pranayb4115
@pranayb4115 4 жыл бұрын
Clear & concise explanation on I/O vs CPU bound tasks. Looking forward for a video series on "asyncio" library.
@TheFred10000
@TheFred10000 4 жыл бұрын
This channel is criminally underrated.
@vishnusingh4118
@vishnusingh4118 4 жыл бұрын
35:03 Yes yes yes ! Please make the vids on the advance topics as well! That will complete the knowledge-pack and make this video even more fruitful as we'll be able to apply the knowledge end-to-end in a real world setting. Thanks a ton Corey.
@somaloco3701
@somaloco3701 4 жыл бұрын
omgggg I wanted to learn this but didn't because it seemed too advanced. thank you corey, there is no better source to learn from than you, I can learn anything that you teach!
@sanviews9819
@sanviews9819 4 жыл бұрын
One of the best Educator on programming out there ... love from India.
@dataenergy
@dataenergy 3 ай бұрын
clear, concise and with provided example... excellent!!
@decrypt0x7c93
@decrypt0x7c93 2 жыл бұрын
You sir, are a HERO. I don't usually hit the red button, but you deserve it! Thanks!
@ravikumaroruganti2097
@ravikumaroruganti2097 3 жыл бұрын
Explained multi threading concept with perfect example.Thanks Corey!
@elgary9074
@elgary9074 2 жыл бұрын
Long videos yes, but what a great explanation totally worth the time. Thanks a lot.
@jonastan1850
@jonastan1850 4 жыл бұрын
Loved your tutorials! Great content. Easy to follow examples.
@kr8771
@kr8771 4 жыл бұрын
amazing work, as always. really looking forward to the upcoming video.
@auroshisray9140
@auroshisray9140 3 жыл бұрын
Hey Corey as always awesome tutorial. Thanks for explaining it in such easy fashion.
@leondaz7380
@leondaz7380 4 жыл бұрын
I watched the AD till the end and clicked on it, Thanks man
@aashaydhawal7274
@aashaydhawal7274 4 жыл бұрын
absolutely WOW, thanks a ton Corey - this topic couldn't be explained better :)
@Nickelkitten75
@Nickelkitten75 5 ай бұрын
Very clear and concise explanation Kudos Corey
@dinohunter7176
@dinohunter7176 4 жыл бұрын
The examples are so well done and nice explained. Good stuff.
@Antz_411
@Antz_411 4 жыл бұрын
Excellent tutorial! Fast paced and concise. Bravo!!
@sunilthapa6702
@sunilthapa6702 3 жыл бұрын
Awesome Video!...Finally understood Threading in Python...Thanks for the video!
@AR-scorp
@AR-scorp 3 жыл бұрын
Awesome video. Clear and crisp. Thank you.
@smithatubeme
@smithatubeme 4 жыл бұрын
Thanks Corey! Can you please also do a tutorial on dockers?
@bit2easy
@bit2easy Жыл бұрын
Thank you for this. The Thread Pool Executor is really simple to use.
@davidnago7947
@davidnago7947 4 жыл бұрын
@Corey you need an award for the top quality content . keep it up
@dmbrv
@dmbrv 4 жыл бұрын
amazing video. I wanted to save it for later but I couldn't stop watching :)
@pythonhd4169
@pythonhd4169 4 жыл бұрын
Wow great introduction, and actually more than an introduction. I am planning (and really should get around to do that) to adapt my DIY logger that I am developing in my latest videos to work in multi-threaded and multi-processing programs.
@JoeBurnett
@JoeBurnett 4 жыл бұрын
This was a very informative video. As always, thank you for taking the time to create this content.
@spikegrazer5690
@spikegrazer5690 2 жыл бұрын
Awesomely structured and explained ! Weldone
@techcode_man
@techcode_man 4 жыл бұрын
Thank You Corey for another top class tutorial. So easy to understand when you explain it!
@coreyms
@coreyms 4 жыл бұрын
Glad it helped!
@koodauskanava9096
@koodauskanava9096 4 жыл бұрын
This is pure gold. Thanks a lot. Really clear and useful tutorial.
@martinmehaudy9043
@martinmehaudy9043 Жыл бұрын
Amazing! Tried something similar with multiprocess, but will test this one out. Thanks!
@reemaljabari3665
@reemaljabari3665 Жыл бұрын
Thank youu so much Highlyy recommended , you just express it in a simple way
@alexnefayne4266
@alexnefayne4266 Жыл бұрын
Your a great teacher i keep comming back to your lessons!
@26_dharmeshmarathe86
@26_dharmeshmarathe86 Жыл бұрын
You're great. Your video on multithreading helped me to undertand the differncce between multithread & multiprocess. Great explaination!!❤❤
@tuvshinotgo5911
@tuvshinotgo5911 4 жыл бұрын
Stop Corey, you are killing it. Kidding no stop
@jmpresto
@jmpresto 2 жыл бұрын
Awesome resource with clear explanations! Thank you
@bishalgautam3874
@bishalgautam3874 4 жыл бұрын
You are a great tutor! Thanks for uploading the videos.
@CorretorAlyssonGomes
@CorretorAlyssonGomes 4 жыл бұрын
Awesome resource for port scanners. Thank you so much.
threading vs multiprocessing in python
22:31
Dave's Space
Рет қаралды 563 М.
Василиса наняла личного массажиста 😂 #shorts
00:22
Денис Кукояка
Рет қаралды 9 МЛН
5 Good Python Habits
17:35
Indently
Рет қаралды 403 М.
Next-Level Concurrent Programming In Python With Asyncio
19:19
ArjanCodes
Рет қаралды 166 М.
3 PYTHON AUTOMATION PROJECTS FOR BEGINNERS
17:00
Internet Made Coder
Рет қаралды 1,5 МЛН
10 Python Tips and Tricks For Writing Better Code
39:21
Corey Schafer
Рет қаралды 1,3 МЛН
25 nooby Python habits you need to ditch
9:12
mCoding
Рет қаралды 1,7 МЛН
Automate your job with Python
6:07
John Watson Rooney
Рет қаралды 372 М.
Unlocking your CPU cores in Python (multiprocessing)
12:16
mCoding
Рет қаралды 295 М.
Python 101: Learn the 5 Must-Know Concepts
20:00
Tech With Tim
Рет қаралды 1,1 МЛН