Staying Safe When Writing Concurrent Code

In general, it is best to share as little as possible between different threads and processes. Problems tend to start as soon as you try to share objects between different thread or process objects.

Python does include a set of tools for locking and otherwise passing and sharing state when using Thread or Process objects including threading.Lock, threading.Barrier, threading.Semaphore, multiprocessing.Queue, and several others. The aforementioned tools are powerful and can be used to write elegant concurrent programs.

When writing the concurrent code, however, I find it helpful to follow a simple heuristic: share nothing. Even though Python provides powerful tools for writing concurrent programs, they ...

Get Intuitive Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.