Skip to content

Introduction to multithreading in Python

A Python application runs on a single thread, unless you explicitly enable multithreading.

Why is multithreading useful? Code in Python is ran in sequence, one instruction after another.

If you define a function that sleeps 3 seconds and then prints something, like this:

import time

def greet():
    time.sleep(3)
    print('hello')

greet()
print('world')

The world string is printed after 3 seconds, because we call time.sleep(3) inside the greet() function.

This is just a silly example, but imagine processing an image, getting a resource from the network, or writing a big file to disk. Anything that can take a lot of time.

With multithreading we can run the function that takes a lot of time into a separate thread, and go on with our program in the meantime.

The threading standard library module helps with implementing multithreading. You import Thread from it:

from threading import Thread

Then we pass the function we must execute as the target argument to the Thread() function, getting a thread object:

t = Thread(target=greet)

then we call its start() method to start the thread:

t.start()

Try running this code:

from threading import Thread
import time

def greet():
    time.sleep(3)
    print('hello')

t = Thread(target=greet)
t.start()

print('world')

You will now see world printed 3 seconds before hello shows up in the console.

The program does not end until the thread (or all the threads it started) end, unless you start a thread as a deamon.

Those are the basics of multithreading. This is complex, and can lead to the introduction of bugs if not done well.


→ Get my Python Handbook
→ Get my Python Handbook

download all my books for free

  • javascript handbook
  • typescript handbook
  • css handbook
  • node.js handbook
  • astro handbook
  • html handbook
  • next.js pages router handbook
  • alpine.js handbook
  • htmx handbook
  • react handbook
  • sql handbook
  • git cheat sheet
  • laravel handbook
  • express handbook
  • swift handbook
  • go handbook
  • php handbook
  • python handbook
  • cli handbook
  • c handbook

subscribe to my newsletter to get them

Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing [email protected]. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.

Related posts about python: