Sitemap
DataFairy

DataFairy

Senior Data Engineer, Azure Warrior, PhD in Theoretical Physics, The Netherlands. I write about Data Engineering, Machine Learning and DevOps on Azure.

Experimenting with Python threads

How to thread and how not to thread in Python

4 min readJul 8, 2024

--

Introduction

Understanding threads in Python is crucial when processing with outputs from events such as I/O tasks. Using threads allows us to write code that potentially runs faster than without the threading library. When working with threads it is important to understand that threads in Python work differently from threads in Java for instance due to the limitation enforced by the GIL. This requires Python developers to thread carefully to avoid common pitfalls.

This is how a thread executes as a function of time:

A Practical Guide to Python Threading By Examples (pythontutorial.net)

What are threads in Python?

A thread is a separate flow of execution within a program that can run concurrently with other threads, allowing multiple operations to (appear to) occur simultaneously.

import threading

def hello_world():
print("Hello, world!")

t = threading.Thread(target=hello_world)
t.start()

Multithreading in Python

Python virtual machine is not a thread-safe interpreter, meaning that the interpreter can execute only one thread at any given moment. This limitation is enforced by the Python Global Interpreter Lock (GIL), which essentially limits one Python thread to run at a time. In other words, GIL ensures that only one thread runs within the same process at the same time on a single processor.

Basically, threading may not speed up all tasks.

import threading
import time

# Function to be executed in the thread
def print_numbers():
for i in range(5):
time.sleep(1) # Simulating some work
print(f"Thread 1: {i}")

def print_letters():
for letter in "abcde":
time.sleep(1) # Simulating some work
print(f"Thread 2: {letter}")

# Create two threads
t1 = threading.Thread(target=print_numbers)
t2 = threading.Thread(target=print_letters)

# Start the threads
t1.start()
t2.start()

# Wait for both threads to finish
t1.join()
t2.join()

print("Done!")
Thread 1: 0
Thread 2: a
Thread 2: b
Thread 1: 1
Thread 2: c
Thread 1: 2
Thread 1: 3
Thread 2: d
Thread 1: 4
Thread 2: e
Done!

When should threads be used?

Good candidates: Tasks that spend much of their time waiting for external events such as I/O bound tasks.

Bad candidates: Problems that require heavy CPU computation and spend little time waiting for external events.

Pitfalls

only one thread in Python:

As mentioned above only one thread can execute simultaneously in Python. To make multiple threads work some smart coding is required:

# Using lists
threads = list()
for index in range(3):
logging.info("Main : create and start thread %d.", index)
x = threading.Thread(target=thread_function, args=(index,))
threads.append(x)
x.start()

for index, thread in enumerate(threads):
logging.info("Main : before joining thread %d.", index)
thread.join()
logging.info("Main : thread %d done", index)

# Using ThreadPoolExecutor
with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
executor.map(thread_function, range(3))

multiple background threads cause exceptions:

Providing the argument daemon=True to a thread will flag it as a background thread. This thread will terminate when the VM/container it is running on terminates. The only issue with this is that if more than one thread is flagged as background thread the entire program will not work.

This is code that runs two threads in the foreground:

import threading


def countSeconds():
print("Second")
threading.Timer(1, countSeconds).start()

def countTenSeconds():
print("Ten seconds passed")
threading.Timer(10, countTenSeconds).start()


threading.Timer(1, countSeconds).start()
threading.Timer(10, countTenSeconds).start()

The output is something like this:

Ten seconds passed
Second
Second
Second
Second
Second
Second
Second
Second
Second
Second

When running the first function with daemon=True

def countSeconds():
print("Second")
threading.Timer(1, countSeconds, daemon=True).start()

The result is the following:

Ten seconds passed
Ten seconds passed

Adding daemon=True to both functions result in multiple exceptions:

TypeError: Timer.__init__() got an unexpected keyword argument 'daemon'
Ten seconds passed
Exception in thread Thread-2:
Traceback (most recent call last):
File "/usr/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
self.run()
File "/usr/lib/python3.10/threading.py", line 1378, in run
self.function(*self.args, **self.kwargs)
File "/mnt/c/python_threading_plg/src/count.py", line 10, in countTenSeconds
threading.Timer(10, countTenSeconds, daemon=True).start()
TypeError: Timer.__init__() got an unexpected keyword argument 'daemon'

This is something to keep in mind when combining multiple threads in Python.

Get DataFairy’s stories in your inbox

Join Medium for free to get updates from this writer.

race conditions:

When two or more threads are trying to access a shared resource we talk about race conditions. An example would be two threads writing to a database simultaneously.

deadlocks:

When working with locks in threads we can acquire a lock and release a lock. We cannot acquire a lock that has not been released first or the resource has to be unlocked.

When two acquire operations are run without a release operation we end up in a deadlock.

Interesting facts on the threading library in Python:

  1. threading.Lock synchronizes threads and allows only one thread at a time to access a resource.
  2. The Python threading library takes advantage of only one CPU (or core) simultaneously.
  3. The Thread.join() method does wait for the thread to finish.
  4. Race conditions are two threads incorrectly accessing a shared resource.
  5. Using ThreadPoolExecutor as a context manager is a good idea cause it manages the start up of all threads and waits for them all to complete.
  6. Once you start a threading.Timer, you can stop it before it expires.

threading vs multiprocessing

Threading is a form of concurency that happens on a single processor. Multiprocessing in contrast can make use of all the available cores.

Multiprocessing becomes interesting when you have to manage I/O bound problems or CPU-heavy workloads. The second is self-explanatory while the first deals with slow network connections or file systems.

To solve the I/O problem we would be overlapping the times spent waiting for the i/o devices. For the CPU problem we would have to find ways to do more computations in the same amount of time.

resources

Code examples:

References:

  1. An Intro to Threading in Python — Real Python
  2. Speed Up Your Python Program With Concurrency — Real Python
  3. Multithreading in Python: The Ultimate Guide (with Coding Examples) (dataquest.io)

If you found this article useful, clap, comment and please follow me.

--

DataFairy
DataFairy

Written by DataFairy

Senior Data Engineer, Azure Warrior, PhD in Theoretical Physics, The Netherlands. I write about Data Engineering, Machine Learning and DevOps on Azure.