Skip to content

Commit fa08431

Browse files
committed
2 parents 0b07920 + 977f675 commit fa08431

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

README.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
# Python Concurrent (thread-safe) collections
2-
3-
Thread-safe Python collections: `ConcurrentBag`, `ConcurrentDictionary`, and `ConcurrentQueue`.
4-
52
## tl;dr
63

7-
Python's built-in `list`, `dict`, and `deque` are thread-safe for some operations, but not all.
4+
Despite what many people think, Python's built-in `list`, `dict`, and `deque` are thread-safe for [_some operations_, but not all](https://docs.python.org/3/faq/library.html#what-kinds-of-global-value-mutation-are-thread-safe). This created a lot of confusion in the Python community.
85

96
`concurrent_collections` provides thread-safe alternatives by using locks internally to ensure safe concurrent access and mutation from multiple threads.
107

8+
Inspired from the amazing [C#'s concurrent collections](https://learn.microsoft.com/en-us/dotnet/api/system.collections.concurrent?view=net-9.0).
9+
1110
## Why use these collections?
1211

1312
**_There is a lot of confusion on whether Python collections are thread-safe or not_**<sup>1, 2, 3</sup>.
1413

1514
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.
1615

17-
See the [Python FAQ: "What kinds of global value mutation are thread-safe?"](https://docs.python.org/3/faq/library.html#what-kinds-of-global-value-mutation-are-thread-safe) for details. The FAQ explains that only a handful of simple operations are guaranteed to be atomic and thread-safe. For anything more complex, you must use your own locking or a thread-safe collection.
16+
See the [Python FAQ: "What kinds of global value mutation are thread-safe?"](https://docs.python.org/3/faq/library.html#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.
17+
The docs even go as far as to say:
18+
19+
> When in doubt, use a mutex!
20+
21+
Which is telling.
1822

19-
I could not find any simple concurrent implementation, so `concurrent_collections` provides drop-in replacements that handle locking for you, making concurrent programming safer and easier.
23+
`concurrent_collections` provides drop-in replacements that handle locking for you, making concurrent programming safer and easier.
24+
Suggestions and feedbacks are welcome.
2025

2126
<sub>
2227

@@ -33,13 +38,13 @@ I could not find any simple concurrent implementation, so `concurrent_collection
3338
Pip:
3439

3540
```bash
36-
pip install python-nameof
41+
pip install concurrent_collections
3742
```
3843

3944
My recommendation is to always use [`uv`](https://docs.astral.sh/uv/) instead of pip – I personally think it's the best package and environment manager for Python.
4045

4146
```bash
42-
uv add python-nameof
47+
uv add concurrent_collections
4348
```
4449

4550
## Collections

0 commit comments

Comments
 (0)