Scenario intermediate

Writing Thread-Safe Python Code

Fix race conditions and corrupted data in a multithreaded Python service by using locks, thread-safe collections, and TTL caches.

2 min read ~15 min to complete
Steps
1 Services Used
~15 min Duration
Intermediate Difficulty
The Situation

Senior Python developer / backend engineering interviews

Context: A Python service uses a shared in-memory cache (dict) that is accessed and updated by multiple threads. You’re seeing occasional KeyError and corrupted data. How do you fix it?

# BROKEN — dict is not thread-safe for concurrent reads/writes
import threading

cache = {}   # Shared across threads

def get_or_compute(key: str):
    if key not in cache:          # Thread A reads: not in cache
        value = expensive_compute(key)   # Both threads compute!
        cache[key] = value        # Race condition: both write
    return cache[key]

# Two threads can both see 'key not in cache' and both compute + write
# FIX — threading.Lock for mutual exclusion
import threading

cache = {}
lock = threading.Lock()

def get_or_compute(key: str):
    with lock:
        if key in cache:
            return cache[key]
        value = expensive_compute(key)  # Only one thread computes
        cache[key] = value
        return value

# FIX 2 — Use threading.RLock for reentrant code (same thread can acquire multiple times)
rlock = threading.RLock()

# FIX 3 — Use concurrent.futures or thread-safe collections
from queue import Queue

task_queue = Queue()   # Thread-safe FIFO

# FIX 4 — cachetools for thread-safe LRU cache
from cachetools import TTLCache
from cachetools.keys import hashkey
import threading

cache = TTLCache(maxsize=1000, ttl=300)  # LRU + TTL
cache_lock = threading.Lock()

def get_or_compute(key: str):
    with cache_lock:
        if key not in cache:
            cache[key] = expensive_compute(key)
        return cache[key]
Services Used
Python
Prerequisites
  • Python 3.10+
  • Basic understanding of async programming
What You Learned
  • Why plain dicts aren't thread-safe
  • threading.Lock and RLock
  • Thread-safe collections and TTL caches

Add More Questions to This Guide

Know a question that should be here? Share it and help the community!

Open Google Form