Published Jun 3, 2022Updated Dec 21, 2022
Contribute to Docs

The threading module allows multiple threads of execution to take place in a Python program.

While threads may appear to run simultaneously, only one thread can be executed at a time. This is enforced by Python’s global interpreter lock.

Threading is helpful when working with tasks that are I/O bound. This includes web-oriented tasks like scraping or downloading files.


import threading

The threading module must first be imported before thread constants can be created and their methods can be used.


The following example features five threads that are created, started, and end at different points before the program finishes:

import threading, time, random
# simulates waiting time (e.g., an API call/response)
def slow_function(thread_index):
time.sleep(random.randint(1, 10))
print("Thread {} done!".format(thread_index))
def run_threads():
threads = []
for thread_index in range(5):
individual_thread = threading.Thread(target=slow_function, args=(thread_index,))
# at this point threads are running independently from the main flow of application and each other
print("Main flow of application")
# This ensures that all threads finish before the main flow of application continues
for individual_thread in threads:
print("All threads are done")

This results in output like the following:

Main flow of application
Thread 1 done!
Thread 4 done!
Thread 3 done!
Thread 2 done!
Thread 0 done!
All threads are done


Returns True if the thread is still running and False, otherwise.
Delays the flow of execution of a program until the target thread is completely read.
Returns a local thread object with data that is specific to that thread.
Executes any target function belonging to a given thread object that is now active.
Activates and prompts a thread object to be run.
Returns a thread object that can run a function with zero or more arguments.

All contributors

Looking to contribute?

Learn Python on Codecademy