You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+13-8Lines changed: 13 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,22 +1,27 @@
1
1
# Python Concurrent (thread-safe) collections
2
-
3
-
Thread-safe Python collections: `ConcurrentBag`, `ConcurrentDictionary`, and `ConcurrentQueue`.
4
-
5
2
## tl;dr
6
3
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.
8
5
9
6
`concurrent_collections` provides thread-safe alternatives by using locks internally to ensure safe concurrent access and mutation from multiple threads.
10
7
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
+
11
10
## Why use these collections?
12
11
13
12
**_There is a lot of confusion on whether Python collections are thread-safe or not_**<sup>1, 2, 3</sup>.
14
13
15
14
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.
16
15
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.
18
22
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.
20
25
21
26
<sub>
22
27
@@ -33,13 +38,13 @@ I could not find any simple concurrent implementation, so `concurrent_collection
33
38
Pip:
34
39
35
40
```bash
36
-
pip install python-nameof
41
+
pip install concurrent_collections
37
42
```
38
43
39
44
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.
0 commit comments