Skip to main content

A brief description of concurrent-collections

Project description

Python Concurrent (thread-safe) collections

Thread-safe Python collections: ConcurrentBag, ConcurrentDictionary, and ConcurrentQueue.

tl;dr

Python's built-in list, dict, and deque are thread-safe for some operations, but not all.

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

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

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.

  1. Are lists thread-safe?

  2. Google style guide advises against relying on Python's assignment atomicity

  3. What kind of "thread safe" are deque's actually?

Installation

Pip:

pip install python-nameof

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 python-nameof

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

concurrent_collections-1.0.1.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

concurrent_collections-1.0.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file concurrent_collections-1.0.1.tar.gz.

File metadata

  • Download URL: concurrent_collections-1.0.1.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.3

File hashes

Hashes for concurrent_collections-1.0.1.tar.gz
Algorithm Hash digest
SHA256 06f84d2911ff50cee0b4303d97bac5f3977a6ce3efddf4f6d9bfb46e85078af5
MD5 6de130b74293e2593c665856e9ce6bde
BLAKE2b-256 5b312fe66fbd20c61357a2a009851309b882094590ce07f2bf86a5ab38d00c58

See more details on using hashes here.

File details

Details for the file concurrent_collections-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for concurrent_collections-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 97eb59e4770db4062517a6b12a662dcf90186c1134c1406d6194e31a4692dd73
MD5 29e88c035aad1f544bd577e940955c87
BLAKE2b-256 c5951863a459913d4e829c4dc1375d970073cf1f7b0aeea68588b036df51033f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page