

In this video, we will be learning how to use threads in Python.
This video is sponsored by Brilliant. Go to 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:
List Comprehensions Tutorial –
Exception Handling Tutorial –
✅ Support My Channel Through Patreon:
✅ Become a Channel Member:
✅ One-Time Contribution Through PayPal:
✅ Cryptocurrency Donations:
Bitcoin Wallet – 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet – 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet – MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot
✅ Corey’s Public Amazon Wishlist
✅ Equipment I Use and Books I Recommend:
▶️ You Can Find Me On:
My Website –
My Second Channel –
Facebook –
Twitter –
Instagram –
#Python
source
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!
hi how are you . thank you very much for video . but when i work with threads , i can not reduce my time program. ex:
……
j=0
for i in range(100000000):
j+=i**2
……
without thread :39s
with thread:39s
with concurrent:39s
i do not know why ?
god willing
17:20 Uhm why did you make it a return statement, i didn't quite understand
Why did the 'done sleeping…' string print out wrong 15:44
Bro this is actually really good thank you
Really interesting. Thank you. I have a code that I am working on that uses OpenCV and two masks that sample a region of interest. I am using thresholding on pixel values to detect motion as a vehicle travels down a road. Not only do I want to know if a car passes but I want to know if it is heading eastbound or westbound. Once motion is detected in one mask, I want to know then it is detected in the second mask. After reviewing your video it would seem to me that this threading approach would be better than using the sleep synchronous approach Thoughts?
Hi Corey, thanks for all your videos! I am trying to introduce multi-threading into my flask application to perform some calculation + operations with the database but I cannot reference current_user or my global db variable from inside the thread subroutine. Maybe you know how to deal with this? I guess it must have something to do with the change of context but I don't know what to do… Thanks again for your awesome work 🙂
Very good tutorial. Thanks Corey.
Hi, i have a query
I want a python script to run at boot time but not the whole program just one thread, SO can anyone please help me it that
I would give you two thumbs up if I could
I'm getting an """"AttributeError: module 'concurrent' has no attribute 'futures' """" error. I couldn't figure out why. I'm on python 3.6, using powershell
Can you plz tell me how to convert two files into thread and then combine them into a third file and all the file are python files
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.
Hi can anyone help..I am trying to make a loop to copy and classify images i have according to category and image index number from a csv file, however I am only managing to keep looping in the same spot ;/ ..this is the code..i tried glob , index , os.walk, iterrows, itertuples, everything. Can someone please indicate what I'm doing wrong. pls pls lps help i am getting so frustrated and annoyed at this.
import glob
import shutil
import pandas as pd
import numpy as np
import os
df=pd.read_csv('Data_entry_2017.csv')
#df.head(12)
#a
a=(df.iloc[0,0])
i=1
#b
b=(df.iloc[0,i])
#Cardiomegaly
#print(type(new_str2))
src = (r'L:/xrayChestImages/images_001/images/') #00000001_000
dest = (r"L:/chest_xray/")
for ind in df.index:
if b == 'Cardiomegaly':
new_str=str(a)
new_str2=str(b)
dest2=os.path.join(dest, new_str)
src2=os.path.join(src, new_str)
print(new_str)
print (src2)
print (dest2)
shutil.copy2(src2, dest2) #file #dest_dir3 1st row
i=i+1
b=(df.iloc[0,i])
@corey quick question, if your machine is multi-core, would not it still benefit, running cpu bound processes in multi-threaded ?
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!
Is it safe for multiple threads to append to the same file?
If you have for example, 500 images to download. It's bad to create 500 threads. How would you limit this to 10 threads at a time?
where the hell did the second time sleep come from
Literally I am learning form your videos..god bless you .
Could've increased ur voice a little bit😄
this guy is a god among us
pretty wonderful video! I only miss an exemple… the possible to limit a 'max thread' during the loop of imagens to download… In a way that, for exemplo, aways has 3 threads (images downloading) working, downloading another image just after finish one.
It would be more appropriate to choose 'futures' instead of 'results' in 21:55, because executor.submit returns future objects that are not guaranteed to be complete
futures = [executor.submit(do_something, sec) for sec in secs]
Another interesting usage of the threading module can be with the smtp library, for an email spammer. Logging in with one email and then spamming someone? Weak. Using multiple threads to have basically no downtime to spamming someone? Chad move.
Does he mean "sequentially" when he says synchronously?
You have saved my project! I was looking for the return of a thread process everywhere! Thank you!
Hi, if i'm trying to extract text from a pdf with 1000 pages into one list. Should i use multi threading or processing? For now it took like 3 minutes to extract the text. Thanks a lot
Awesome Video!…Finally understood Threading in Python…Thanks for the video!
Watching this video after a long time, still quite helpful!
1:38 Thank you so much I've tried before to learn multithreading for python and make it work like go but this time I finally understand it
Thank you so much for this video. I ddin't actually know how threads work, and you explained it very well!
Hey, I encountered some unexpected results when trying your download_images.py Script. When using the concurrent.futures.ThreadPoolExecutor() with Mal function, I get around 21 seconds as time needed to Run this Script as oposed to the non threaded solution with 23 Seconds. I confirmed this one 2 Systems running python3.8 and the exact script you provided in your code snippets. Any Suggestion what might be missing?
Btw I Love your explanation, I doubt i will ever look for any other Python instructor.
First: Nice job!!! +1 member from Brazil!
Second: I like to know if the Threading module can handle any "crazy dev" trying to launch a thousand of threads at the same time. It will throw an error or will auto "pipeline" the execution of the threads?
Anyone else getting times of 1.66s (.1s longer or so) here? (14:31)
I get such varianace, wondering how common it is.
Awesome, simply awesome and had to watch ads all the way to the end in appreciation this level of quality.
Thanks
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
I dont really like the concurrent module when you have separate methods for threading and processing also they have pool methods
For instance, if i have to pass multiple parameters in the target function, how do i do using ThreadPoolExecutors?
eg:
def process_items(item, storage, seconds, numbers):
if numbers != 5:
storage.append(item)
else:
time.sleep(seconds)
Corey Your ability to teach is amazing.
Nice video! Would love to see one on asyncio and async/await to go along with this one and the multiprocessing video.
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.
Explained multi threading concept with perfect example.Thanks Corey!
You're my favorite. These are amazing.
What's the difference between multi threads and asynchronous calls?