atomicx
atomicx is an easy-to-use atomics library for Python, providing atomic integer, boolean, and floats. It allows you to perform atomic operations on shared variables, ensuring thread-safety and preventing race conditions in concurrent programming. Everything is entirely lock-free and is backed by Rust's atomic types (or from portable-atomic). Builds are published for the standard GIL runtime and the free-threaded CPython variants.
Features
- Atomic integer operations: load, store, add, subtract, swap, compare and exchange, multiply, divide, increment, decrement.
- Atomic boolean operations: load, store, swap, compare and exchange, flip.
- Atomic floats: load, store, swap, compare and exchange.
- Strong typing provided as stubs for static type checkers.
Installation
Binary wheels are provided for Python 3.8 and above on Linux, macOS, and Windows:
pip install atomicx
Usage
See the documentation for more information. Here's a quick overview:
Atomic Integer
from atomicx import AtomicInt
# Create an atomic integer with an initial value of 0
atom = AtomicInt()
# Perform atomic operations
atom.store(10)
value = atom.load()
print(f"Value: {value}")
previous_value = atom.swap(20)
print(f"Previous Value: {previous_value}")
atom.add(5)
print(f"Result after addition: {atom}")
# Run a retry loop to clamp the value to at least 0
atom.update(lambda current: max(current, 0))
# Increment and decrement operations
atom.inc()
atom.dec()
Atomic Boolean
from atomicx import AtomicBool
# Create an atomic boolean with an initial value of False
atom = AtomicBool()
# Perform atomic operations
atom.store(True)
value = atom.load()
print(f"Value: {value}")
previous_value = atom.swap(False)
print(f"Previous Value: {previous_value}")
result = atom.compare_exchange(False, True)
print(f"Swap Result: {result}")
# Flip the value of the atomic boolean
atom.flip()
# Update using a callable
atom.update(lambda current: not current)
Atomic Float
from atomicx import AtomicFloat
# Create an atomic float with an initial value of 0.0
atom = AtomicFloat()
print(f"Initial Value: {atom.load()}")
# Perform atomic operations
atom.store(3.14)
value = atom.load()
print(f"Value: {value}")
# See docs for more operations
Thread Safety
Atomic variables are thread-safe and can be shared between threads. Each predefined operation on them are executed per thread as an indivisible unit. Here's an example of using an atomic integer in a multithreaded environment:
import threading
from atomicx import AtomicInt
x = AtomicInt(0)
def increment():
for _ in range(1000):
x.inc() # equivalent to x.add(1) or x += 1
threads = []
for _ in range(10):
thread = threading.Thread(target=increment)
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
assert x.load() == 1000 * 10
The equivalent in vanilla Python without locks is not thread-safe in general.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Acknowledgments
- The
atomicxlibrary is heavily dependent on and inspired by the Ruststd::sync::atomicmodule. - The floating point atomic operations are delegated to the
portable-atomiccrate.
Metadata
Release files for atomicx 0.0.15
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| atomicx-0.0.15.tar.gz | 15.9 kB | Details |
Built distributions (wheels)
| File | Reset | |||
|---|---|---|---|---|
| atomicx-0.0.15-cp313-cp313t-win_amd64.whl | CPython 3.13 | CPython 3.13 free-threading | Windows x86-64 | Details |
| atomicx-0.0.15-cp313-cp313t-manylinux_2_34_x86_64.whl | CPython 3.13 | CPython 3.13 free-threading | Linux glibc 2.34+ x86-64 | Details |
| atomicx-0.0.15-cp313-cp313t-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl | CPython 3.13 | CPython 3.13 free-threading | macOS 10.12+ universal2 (ARM64, x86-64), macOS 10.12+ x86-64, macOS 11.0+ ARM64 | Details |
| atomicx-0.0.15-cp312-cp312-win_amd64.whl | CPython 3.12 | CPython 3.12 | Windows x86-64 | Details |
| atomicx-0.0.15-cp311-cp311-win_amd64.whl | CPython 3.11 | CPython 3.11 | Windows x86-64 | Details |
| atomicx-0.0.15-cp310-cp310-win_amd64.whl | CPython 3.10 | CPython 3.10 | Windows x86-64 | Details |
| atomicx-0.0.15-cp39-cp39-win_amd64.whl | CPython 3.9 | CPython 3.9 | Windows x86-64 | Details |
| atomicx-0.0.15-cp38-abi3-win_amd64.whl | CPython 3.8 | abi3 | Windows x86-64 | Details |
| atomicx-0.0.15-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl | CPython 3.8 | abi3 | Linux glibc 2.17+ x86-64 | Details |
| atomicx-0.0.15-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl | CPython 3.8 | abi3 | macOS 10.12+ x86-64, macOS 11.0+ ARM64, macOS 10.12+ universal2 (ARM64, x86-64) | Details |
Total release size: 2.6 MB
Release files / atomicx-0.0.15.tar.gz
| Download URL | atomicx-0.0.15.tar.gz |
|---|---|
| Size | 15.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6e1f90e569e0ed61109907fb05689c67ba95c97e99496773c90040e10826b035
|
|
BLAKE2b-256 checksum How to use checksums |
2c2d9db45a9d9bb294239575730b31ac3e65de2d987364f27d9ec05215af7c49
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.9.4
|
Release files / atomicx-0.0.15-cp313-cp313t-win_amd64.whl
| Download URL | atomicx-0.0.15-cp313-cp313t-win_amd64.whl |
|---|---|
| Size | 156.0 kB |
| Tags | CPython 3.13 CPython 3.13 free-threading Windows x86-64 |
|
SHA-256 checksum How to use checksums |
5f891d06ab424366ec2f8aef3c697715aabd443b83955d8d427ebdbb123b2900
|
|
BLAKE2b-256 checksum How to use checksums |
480e8680ccb16f9c808cafa35fb737de78052d2267cd2418f76d8e88760f1a59
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.9.4
|
Release files / atomicx-0.0.15-cp313-cp313t-manylinux_2_34_x86_64.whl
| Download URL | atomicx-0.0.15-cp313-cp313t-manylinux_2_34_x86_64.whl |
|---|---|
| Size | 295.0 kB |
| Tags | CPython 3.13 CPython 3.13 free-threading Linux glibc 2.34+ x86-64 |
|
SHA-256 checksum How to use checksums |
d3e9d9edf09bc0ad4e093d448df00cc3cee9312698331664eb1752b86b507776
|
|
BLAKE2b-256 checksum How to use checksums |
f58ecc60709adde38969d590f7b61585cf474434db9c46faae29a2b009a4d22e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.9.4
|
Release files / atomicx-0.0.15-cp313-cp313t-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
| Download URL | atomicx-0.0.15-cp313-cp313t-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl |
|---|---|
| Size | 519.9 kB |
| Tags | CPython 3.13 CPython 3.13 free-threading macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
2a21f183742635275a3aeb2aec014f58f7dd18e96d20bd5f582113dcd305e6eb
|
|
BLAKE2b-256 checksum How to use checksums |
cf339950b57d43c5d2b4168e9ed3443d6a6252ed7f51fed1dbf14b20956d3f16
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.9.4
|
Release files / atomicx-0.0.15-cp312-cp312-win_amd64.whl
| Download URL | atomicx-0.0.15-cp312-cp312-win_amd64.whl |
|---|---|
| Size | 156.8 kB |
| Tags | CPython 3.12 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
562d4de7183529065ee8ea82046eeb2b4bbddeb7771b8b87658e934143c34f50
|
|
BLAKE2b-256 checksum How to use checksums |
ecacd783bbe3ff3a1c0c730204ab33541c3c486270afb0c8a14bfd9479ad3f74
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.9.4
|
Release files / atomicx-0.0.15-cp311-cp311-win_amd64.whl
| Download URL | atomicx-0.0.15-cp311-cp311-win_amd64.whl |
|---|---|
| Size | 155.4 kB |
| Tags | CPython 3.11 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
15354cff1fee57ffc183f90cd3b51e62b1cc391ec16acc288128eed9f51d8865
|
|
BLAKE2b-256 checksum How to use checksums |
4b38c2e84de8ccc869c199b12054f6b08788dbf87762cb480b5263c79881e30a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.9.4
|
Release files / atomicx-0.0.15-cp310-cp310-win_amd64.whl
| Download URL | atomicx-0.0.15-cp310-cp310-win_amd64.whl |
|---|---|
| Size | 155.2 kB |
| Tags | CPython 3.10 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
ae128e880fbc6d85c73e46f666bb39b8e103f7a528006e60b16833a3690df732
|
|
BLAKE2b-256 checksum How to use checksums |
1dd345e6e6046c28237f041269397bccbea45eb2d6933e5750196e8d95e3b909
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.9.4
|
Release files / atomicx-0.0.15-cp39-cp39-win_amd64.whl
| Download URL | atomicx-0.0.15-cp39-cp39-win_amd64.whl |
|---|---|
| Size | 156.2 kB |
| Tags | CPython 3.9 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
695a4fbc93ea5b6dd5d5b9a19a0406156fb9c698c733667a04d748248c97089c
|
|
BLAKE2b-256 checksum How to use checksums |
d89820cf5bcca4d684a2baf17117ab4db58049f99e8b30ec8f201cae8f0d48ac
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.9.4
|
Release files / atomicx-0.0.15-cp38-abi3-win_amd64.whl
| Download URL | atomicx-0.0.15-cp38-abi3-win_amd64.whl |
|---|---|
| Size | 158.8 kB |
| Tags | CPython 3.8 Windows x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
32ce46bcdc328b12780084773536ecedffa6fecb1ad4cf30b839d694d2e9990e
|
|
BLAKE2b-256 checksum How to use checksums |
4b305b6c95a505e4da794ae5095de67f397742fc5d51729d4ff2f457920a59b9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.9.4
|
Release files / atomicx-0.0.15-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | atomicx-0.0.15-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 304.4 kB |
| Tags | CPython 3.8 Linux glibc 2.17+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
59d5cc5c32756d0429dff20abb2fc1f5eeb3efaa55e73f081ec6b868c542b7e9
|
|
BLAKE2b-256 checksum How to use checksums |
4df0627c353a6945e481c6d9eb57637464b1f7a61962d4b2ea4c97a8f59bc25f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.9.4
|
Release files / atomicx-0.0.15-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
| Download URL | atomicx-0.0.15-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl |
|---|---|
| Size | 537.1 kB |
| Tags | CPython 3.8 abi3 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
07fecd0ca8e177c3d1bab3b4b61f802eeb1336dae952af1f28e21eac090d9268
|
|
BLAKE2b-256 checksum How to use checksums |
a4459de50e8f33d880ae83b6d6909169de4de72ee43623a7b740e5d3bae1cc64
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.9.4
|