Tackling Thread Safety in Python
summary
Demonstrates that naive threading on a shared variable causes a real race: a booking script with 10 seats / 15 customers -> final count -5 because concurrent read/write interleaves. Shows concrete fixes using Python threading: using a Lock fixes to 10 booked; an RLock prevents deadlock for nested acquisitions; a Semaphore example limits concurrent bookings to three (10 workers proceed three at a time). Also covers Event, Barrier, and Condition primitives and when to prefer avoiding shared mutable state.
AI-written summary · about
chapters (12)
- 0:00Introduction to thread safety
- 1:04What is threading in Python
- 3:58Understanding race conditions
- 7:11Thread safety explained
- 9:38Synchronization primitives overview
- 10:20Using locks for thread safety
- 13:33Re-entrant locks and deadlocks
- 17:06Semaphores for resource control
- 21:48Event primitives for signaling
- 23:11Barriers for thread coordination
- 24:57Condition variables explained
- 28:34Best practices and summary
related talks
35mWriting robust, readable, and maintainable concurrent programs in Python
47mWhat All Rubyists Should Know About Threads
31mPython Multithreading and Multiprocessing: Concurrency and Parallelism
48mHandling concurrent access to shared resources in Java
50mUsing Asyncio in Python 3
42mLook out! Gotchas of Using Threads in Ruby
54mWhat do you mean "thread-safe"?
38mImplementing a decorator for thread synchronisation
