Python Threading Tutorial: Run Code Concurrently Using the Threading Module

  Рет қаралды 821,440

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
@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!
@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 !
@truongtran99_
@truongtran99_ Ай бұрын
Your explanation about threading was so clear and easy to understand. Thank you for making this video!
@EdiFerreiranet
@EdiFerreiranet 4 жыл бұрын
10:54 Throw away variable!?! Nice!!! I didn't knew that. Awesome!!! Thank you. You're the best ;)
@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!
@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.
@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.
@gamingwithpawan9455
@gamingwithpawan9455 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!
@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!
@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!
@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.
@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
@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
@dano2169
@dano2169 4 жыл бұрын
Literally so many videos out there on threading and multiprocessing but by far this is the best. Thanks Corey!
@chazyua
@chazyua 4 жыл бұрын
OMG. This is not the first time you read my mind. Literally just wanted to find some threading topics ! Wow ! Thanks
@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.
@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
@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!
@shinobi9461
@shinobi9461 4 жыл бұрын
Your tutorials are truly phenomenal. Thank you so much for sharing these videos with everyone, they’re incredible!
@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
@fredpaik1349
@fredpaik1349 4 жыл бұрын
Thank you Corey for ALL your videos! You are an inspiration and a great teacher. Keep up the good work!
@DarshitParmar
@DarshitParmar 4 жыл бұрын
You are an angel. Most youtubers teach textbook. Thank you so much for this tutorial!
@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
@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
@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...
@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.
@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
@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.
@touchpad24
@touchpad24 2 жыл бұрын
Your explanation is so simple yet at the same time covers everything. Thanks so much!
@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
@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.
@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.
@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.
@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.
@spoonmanchen7631
@spoonmanchen7631 4 жыл бұрын
You never let me down, My Instructor. Always looking forward to your next video.
@TheFred10000
@TheFred10000 4 жыл бұрын
This channel is criminally underrated.
@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.
@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.
@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
@jerry5646
@jerry5646 4 жыл бұрын
Hands down. Best python tutorial in youtube
@jaehochoi8062
@jaehochoi8062 Жыл бұрын
Clear, easy, pinpoint, reliable plus truly helpful explanation ever!
@newpain01
@newpain01 Жыл бұрын
Completely agree about the basic example tutorials, I often find them useless. Great video, thanks!
@mengzhu8385
@mengzhu8385 3 жыл бұрын
Most clear video on explaining multi-threading in Python!
@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.
@smithatubeme
@smithatubeme 4 жыл бұрын
Thanks Corey! Can you please also do a tutorial on dockers?
@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.
@yuliiakhalus7339
@yuliiakhalus7339 4 жыл бұрын
Thank you very much for this tutorial! No one can explain all this Python stuff better that you!
@jaylocker2055
@jaylocker2055 4 жыл бұрын
LOL, just cut a loop that makes 12,000+ API calls from 42 minutes to < 5 minutes. This is freakin awesome!
@diegomedinaflores3362
@diegomedinaflores3362 3 жыл бұрын
How do you avoid being banned? proxies?
@jaylocker2055
@jaylocker2055 3 жыл бұрын
@@diegomedinaflores3362 I'm hitting an internal service our company uses. Wish I could do batch requests, but the info I needed had to be a per record request as noted in the services documentation.
@tuvshinotgo5911
@tuvshinotgo5911 4 жыл бұрын
Stop Corey, you are killing it. Kidding no stop
@gastonscazzuso4113
@gastonscazzuso4113 3 жыл бұрын
Corey , this is the best explained Threading topic video in KZfaq !.. Awesome.. and many Thanks !
@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!
@matthewson8917
@matthewson8917 4 жыл бұрын
It's awesome that teacher Corey covered what I've been curious about...:)
@leondaz7380
@leondaz7380 4 жыл бұрын
I watched the AD till the end and clicked on it, Thanks man
@kr8771
@kr8771 4 жыл бұрын
amazing work, as always. really looking forward to the upcoming video.
@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 ... ).
@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.
@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!
@aashaydhawal7274
@aashaydhawal7274 4 жыл бұрын
absolutely WOW, thanks a ton Corey - this topic couldn't be explained better :)
@sanviews9819
@sanviews9819 4 жыл бұрын
One of the best Educator on programming out there ... love from India.
@stephenrwafa2519
@stephenrwafa2519 3 жыл бұрын
Awesome, simply awesome and had to watch ads all the way to the end in appreciation this level of quality. Thanks
@arunp07
@arunp07 2 жыл бұрын
Great tutorial on Threading. Thanks, Corey, Explanation is at the perfect pace, not needed to increase or decrease the playback speed.
@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
@alexchung485
@alexchung485 3 жыл бұрын
You have saved my project! I was looking for the return of a thread process everywhere! Thank you!
@koodauskanava9096
@koodauskanava9096 4 жыл бұрын
This is pure gold. Thanks a lot. Really clear and useful tutorial.
@moeinmhzg7827
@moeinmhzg7827 3 жыл бұрын
I don't remember if I saw any better python tutorials. You are the best.
@karansmittal
@karansmittal 3 жыл бұрын
It was a very demanded video, Thanks for working on it. Always creating high quality video content.
@jonastan1850
@jonastan1850 4 жыл бұрын
Loved your tutorials! Great content. Easy to follow examples.
@dmbrv
@dmbrv 4 жыл бұрын
amazing video. I wanted to save it for later but I couldn't stop watching :)
@dataenergy
@dataenergy 2 ай бұрын
clear, concise and with provided example... excellent!!
@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.
@AR-scorp
@AR-scorp 3 жыл бұрын
Awesome video. Clear and crisp. Thank you.
@auroshisray9140
@auroshisray9140 3 жыл бұрын
Hey Corey as always awesome tutorial. Thanks for explaining it in such easy fashion.
@decrypt0x7c93
@decrypt0x7c93 2 жыл бұрын
You sir, are a HERO. I don't usually hit the red button, but you deserve it! Thanks!
@Nickelkitten75
@Nickelkitten75 4 ай бұрын
Very clear and concise explanation Kudos Corey
@walid7189
@walid7189 3 жыл бұрын
wow! never stop doing these videos!
@reemaljabari3665
@reemaljabari3665 Жыл бұрын
Thank youu so much Highlyy recommended , you just express it in a simple way
@Antz_411
@Antz_411 4 жыл бұрын
Excellent tutorial! Fast paced and concise. Bravo!!
@lmao_ur_mum
@lmao_ur_mum 4 жыл бұрын
Me: watching this video My friend sitting next to me: "Are you sure you can sleep only one second?"
@bishalgautam3874
@bishalgautam3874 4 жыл бұрын
You are a great tutor! Thanks for uploading the videos.
@pranayb4115
@pranayb4115 4 жыл бұрын
Clear & concise explanation on I/O vs CPU bound tasks. Looking forward for a video series on "asyncio" library.
@johnnymysto
@johnnymysto 3 жыл бұрын
Great explanation!! I knew basically nothing about this before I watched it, and it makes complete sense now.
@spikegrazer5690
@spikegrazer5690 2 жыл бұрын
Awesomely structured and explained ! Weldone
@JoeBurnett
@JoeBurnett 4 жыл бұрын
This was a very informative video. As always, thank you for taking the time to create this content.
@millaw.1718
@millaw.1718 4 жыл бұрын
This is my prayers coming true! Thanks a lot for explaining this topic
@traxiduswolf4454
@traxiduswolf4454 4 жыл бұрын
I kinda love you for this (and your channel) ... I wrote this before seen the video, I know i will not be dissapointed ❤
@sergioandres8041
@sergioandres8041 3 жыл бұрын
First time in your channel. Your teaching skills are amazing ! straight to the point, perfect examples and nice speech :)
@dinohunter7176
@dinohunter7176 4 жыл бұрын
The examples are so well done and nice explained. Good stuff.
@hotcrossbun26
@hotcrossbun26 Жыл бұрын
I'd be so screwed without your videos. The BEST explanations anywhere on the internet for learning python.
@ahmedabdulrahman8567
@ahmedabdulrahman8567 4 жыл бұрын
your fan from Sudan ... thanks for making such unique content
@davidnago7947
@davidnago7947 4 жыл бұрын
@Corey you need an award for the top quality content . keep it up
@patloeber
@patloeber 4 жыл бұрын
Great Tutorial!
@pahehepaa4182
@pahehepaa4182 3 жыл бұрын
Hands down, best instructor.
@shukailu6731
@shukailu6731 4 жыл бұрын
Thanks Corey, this video was awesome!!
@george4746
@george4746 3 жыл бұрын
Excellent examples, thanks Corey!
threading vs multiprocessing in python
22:31
Dave's Space
Рет қаралды 562 М.
The joker's house has been invaded by a pseudo-human#joker #shorts
00:39
Untitled Joker
Рет қаралды 4,5 МЛН
ИРИНА КАЙРАТОВНА - АЙДАХАР (БЕКА) [MV]
02:51
ГОСТ ENTERTAINMENT
Рет қаралды 4,2 МЛН
터키아이스크림🇹🇷🍦Turkish ice cream #funny #shorts
00:26
Byungari 병아리언니
Рет қаралды 25 МЛН
小女孩把路人当成离世的妈妈,太感人了.#short #angel #clown
00:53
5 Good Python Habits
17:35
Indently
Рет қаралды 395 М.
C++ Developer Learns Python
9:26
PolyMars
Рет қаралды 2,7 МЛН
Next-Level Concurrent Programming In Python With Asyncio
19:19
ArjanCodes
Рет қаралды 165 М.
The 3 Laws of Writing Readable Code
5:28
Kantan Coding
Рет қаралды 290 М.
5 Really Cool Python Functions
19:58
Indently
Рет қаралды 52 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 265 М.
Unlocking your CPU cores in Python (multiprocessing)
12:16
mCoding
Рет қаралды 294 М.
Top 18 Most Useful Python Modules
10:50
Tech With Tim
Рет қаралды 924 М.
The joker's house has been invaded by a pseudo-human#joker #shorts
00:39
Untitled Joker
Рет қаралды 4,5 МЛН