Python Threading Module

The threading module lets you run multiple threads in the same process and coordinate them with locks.

import threading

Threads are useful for waiting on I/O, such as network requests or file operations. They are not usually the best way to speed up CPU-heavy Python code.

Starting a thread

import threading

def worker():
    print('working')

thread = threading.Thread(target=worker)
thread.start()
thread.join()
working

start() begins the thread. join() waits for it to finish before the main program continues.

Passing arguments

import threading

def greet(name):
    print(f'hello {name}')

thread = threading.Thread(target=greet, args=('Ada',))
thread.start()
thread.join()
hello Ada

Use args for positional arguments and kwargs for keyword arguments.

Using a lock

Locks help protect shared state.

import threading

counter = 0
lock = threading.Lock()

with lock:
    counter += 1

print(counter)
1

Running several threads

import threading

results = []
lock = threading.Lock()

def worker(number):
    with lock:
        results.append(f'worker {number}')

threads = []
for number in range(3):
    thread = threading.Thread(target=worker, args=(number,))
    threads.append(thread)
    thread.start()

for thread in threads:
    thread.join()

print(sorted(results))
['worker 0', 'worker 1', 'worker 2']

The actual completion order may vary because threads run concurrently, so this example sorts the result before printing.