Python Threading Tutorial: Run Code Concurrently Using the Threading Module

///Python Threading Tutorial: Run Code Concurrently Using the Threading Module

Python Threading Tutorial: Run Code Concurrently Using the Threading Module

FavoriteLoadingAdd to favorites

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

By |2020-10-26T10:18:54+00:00October 26th, 2020|Python Video Tutorials|45 Comments

45 Comments

  1. Corey Schafer October 26, 2020 at 10:18 am - Reply

    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!

  2. mahdi security October 26, 2020 at 10:18 am - Reply

    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

  3. Moaz Alahmar October 26, 2020 at 10:18 am - Reply

    17:20 Uhm why did you make it a return statement, i didn't quite understand

  4. Gabe Morris October 26, 2020 at 10:18 am - Reply

    Why did the 'done sleeping…' string print out wrong 15:44

  5. R_-G-_F October 26, 2020 at 10:18 am - Reply

    Bro this is actually really good thank you

  6. Russell __ October 26, 2020 at 10:18 am - Reply

    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?

  7. Roberto Pizziol October 26, 2020 at 10:18 am - Reply

    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 🙂

  8. james mwai October 26, 2020 at 10:18 am - Reply

    Very good tutorial. Thanks Corey.

  9. Prem Garg October 26, 2020 at 10:18 am - Reply

    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

  10. Rzaruwa October 26, 2020 at 10:18 am - Reply

    I would give you two thumbs up if I could

  11. Mert İndibi October 26, 2020 at 10:18 am - Reply

    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

  12. Aayush tech & gaming October 26, 2020 at 10:18 am - Reply

    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

  13. Gurudev Ilangovan October 26, 2020 at 10:18 am - Reply

    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.

  14. Mark Caruana October 26, 2020 at 10:18 am - Reply

    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])

  15. Gunaratnam Kuhajeyan October 26, 2020 at 10:18 am - Reply

    @corey quick question, if your machine is multi-core, would not it still benefit, running cpu bound processes in multi-threaded ?

  16. Fergusons of Stirling October 26, 2020 at 10:18 am - Reply

    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!

  17. Tomás Vallotton October 26, 2020 at 10:18 am - Reply

    Is it safe for multiple threads to append to the same file?

  18. J C October 26, 2020 at 10:18 am - Reply

    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?

  19. EXpoGamerz October 26, 2020 at 10:18 am - Reply

    where the hell did the second time sleep come from

  20. a2z October 26, 2020 at 10:18 am - Reply

    Literally I am learning form your videos..god bless you .

  21. Thenga Kola October 26, 2020 at 10:18 am - Reply

    Could've increased ur voice a little bit😄

  22. celery October 26, 2020 at 10:18 am - Reply

    this guy is a god among us

  23. RiptorForever October 26, 2020 at 10:18 am - Reply

    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.

  24. Zh. October 26, 2020 at 10:18 am - Reply

    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]

  25. Javier Heng October 26, 2020 at 10:18 am - Reply

    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.

  26. Karthik B October 26, 2020 at 10:18 am - Reply

    Does he mean "sequentially" when he says synchronously?

  27. Alex Steve Chung Alvarez October 26, 2020 at 10:18 am - Reply

    You have saved my project! I was looking for the return of a thread process everywhere! Thank you!

  28. Guillaume Chandonnet October 26, 2020 at 10:18 am - Reply

    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

  29. Sunil Thapa October 26, 2020 at 10:18 am - Reply

    Awesome Video!…Finally understood Threading in Python…Thanks for the video!

  30. Animesh Ghosh October 26, 2020 at 10:18 am - Reply

    Watching this video after a long time, still quite helpful!

  31. Thomas Forbes October 26, 2020 at 10:18 am - Reply

    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

  32. Shrishail Gharat October 26, 2020 at 10:18 am - Reply

    Thank you so much for this video. I ddin't actually know how threads work, and you explained it very well!

  33. Pikaia Lotti October 26, 2020 at 10:18 am - Reply

    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.

  34. Rafael Gaio October 26, 2020 at 10:18 am - Reply

    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?

  35. Rotem levi October 26, 2020 at 10:18 am - Reply

    Anyone else getting times of 1.66s (.1s longer or so) here? (14:31)
    I get such varianace, wondering how common it is.

  36. Stephen Rwafa October 26, 2020 at 10:18 am - Reply

    Awesome, simply awesome and had to watch ads all the way to the end in appreciation this level of quality.

    Thanks

  37. Borja October 26, 2020 at 10:18 am - Reply

    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

  38. Karthick Durai October 26, 2020 at 10:18 am - Reply

    I dont really like the concurrent module when you have separate methods for threading and processing also they have pool methods

  39. Jugs Ma马家杰 October 26, 2020 at 10:18 am - Reply

    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)

  40. Mostafa Nakhaei October 26, 2020 at 10:18 am - Reply

    Corey Your ability to teach is amazing.

  41. Rory Sullivan October 26, 2020 at 10:18 am - Reply

    Nice video! Would love to see one on asyncio and async/await to go along with this one and the multiprocessing video.

  42. georgesd9 October 26, 2020 at 10:18 am - Reply

    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.

  43. Ravi Kumar October 26, 2020 at 10:18 am - Reply

    Explained multi threading concept with perfect example.Thanks Corey!

  44. Susan C October 26, 2020 at 10:18 am - Reply

    You're my favorite. These are amazing.

  45. Vladimir Gorea October 26, 2020 at 10:18 am - Reply

    What's the difference between multi threads and asynchronous calls?

Leave A Comment

*