A brief description of concurrent-collections
Project description
Python Concurrent (thread-safe) collections
tl;dr
Despite what many people think, Python's built-in list, dict, and deque are thread-safe for some operations, but not all. This created a lot of confusion in the Python community.
concurrent_collections provides thread-safe alternatives by using locks internally to ensure safe concurrent access and mutation from multiple threads.
Inspired from the amazing C#'s concurrent collections.
Why use these collections?
There is a lot of confusion on whether Python collections are thread-safe or not1, 2, 3.
The bottom line is that Python's built-in collections are not fully thread-safe for all operations. While some simple operations (like list.append() or dict[key] = value) are thread-safe due to the Global Interpreter Lock (GIL), compound operations and iteration with mutation are not. This can lead to subtle bugs, race conditions, or even crashes in multi-threaded programs.
See the Python FAQ: "What kinds of global value mutation are thread-safe?" for details. The FAQ explains that only some (if common) operations are guaranteed to be atomic and thread-safe, but for anything more complex, you must use your own locking.
The docs even go as far as to say:
When in doubt, use a mutex!
Which is telling.
concurrent_collections provides drop-in replacements that handle locking for you, making concurrent programming safer and easier.
Suggestions and feedbacks are welcome.
Installation
Pip:
pip install concurrent_collections
My recommendation is to always use uv instead of pip – I personally think it's the best package and environment manager for Python.
uv add concurrent_collections
Collections
ConcurrentBag
A thread-safe, list-like collection.
from concurrent_collections import ConcurrentBag
bag = ConcurrentBag([1, 2, 3])
bag.append(4)
print(list(bag)) # [1, 2, 3, 4]
ConcurrentDictionary
A thread-safe dictionary. For atomic compound updates, use update_atomic.
from concurrent_collections import ConcurrentDictionary
d = ConcurrentDictionary({'x': 1})
d['y'] = 2 # Simple assignment is thread-safe
# For atomic updates:
d.update_atomic('x', lambda v: v + 1)
print(d['x']) # 2
ConcurrentQueue
A thread-safe double-ended queue.
from concurrent_collections import ConcurrentQueue
q = ConcurrentQueue()
q.append(1)
q.appendleft(0)
print(q.pop()) # 1
print(q.popleft()) # 0
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file concurrent_collections-1.0.2.tar.gz.
File metadata
- Download URL: concurrent_collections-1.0.2.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
36d88313ffa7d269c34973b1b3fbf2ef8938efdbb7980bb3ce7641534e5a4697
|
|
| MD5 |
a6c81768e40f564c715c7a1321b91df7
|
|
| BLAKE2b-256 |
30322ea8509219ef3ddc3c97d10d73ebf862367c891d0949be1183db88fb2493
|
File details
Details for the file concurrent_collections-1.0.2-py3-none-any.whl.
File metadata
- Download URL: concurrent_collections-1.0.2-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f1e7af011f07f4d23bb40d98bc3814a4fe9fadc04012aebfd9f227ec19603457
|
|
| MD5 |
d2ad5233c0a91cff1a3971575d3f9521
|
|
| BLAKE2b-256 |
d837becb833de18f5cdfabbea1a260579e6bf555e66bf9ccbd7060e5d35e71fd
|