Skip to main content

easy-to-use lock-free atomic integers, booleans, and floats for Python

Project description

atomicx

PyPI version

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 atomicx library is heavily dependent on and inspired by the Rust std::sync::atomic module.
  • The floating point atomic operations are delegated to the portable-atomic crate.

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

atomicx-0.0.15.tar.gz (15.9 kB view details)

Uploaded Source

Built Distributions

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

atomicx-0.0.15-cp313-cp313t-win_amd64.whl (156.0 kB view details)

Uploaded CPython 3.13tWindows x86-64

atomicx-0.0.15-cp313-cp313t-manylinux_2_34_x86_64.whl (295.0 kB view details)

Uploaded CPython 3.13tmanylinux: glibc 2.34+ x86-64

atomicx-0.0.15-cp313-cp313t-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (519.9 kB view details)

Uploaded CPython 3.13tmacOS 10.12+ universal2 (ARM64, x86-64)macOS 10.12+ x86-64macOS 11.0+ ARM64

atomicx-0.0.15-cp312-cp312-win_amd64.whl (156.8 kB view details)

Uploaded CPython 3.12Windows x86-64

atomicx-0.0.15-cp311-cp311-win_amd64.whl (155.4 kB view details)

Uploaded CPython 3.11Windows x86-64

atomicx-0.0.15-cp310-cp310-win_amd64.whl (155.2 kB view details)

Uploaded CPython 3.10Windows x86-64

atomicx-0.0.15-cp39-cp39-win_amd64.whl (156.2 kB view details)

Uploaded CPython 3.9Windows x86-64

atomicx-0.0.15-cp38-abi3-win_amd64.whl (158.8 kB view details)

Uploaded CPython 3.8+Windows x86-64

atomicx-0.0.15-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (304.4 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ x86-64

atomicx-0.0.15-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (537.1 kB view details)

Uploaded CPython 3.8+macOS 10.12+ universal2 (ARM64, x86-64)macOS 10.12+ x86-64macOS 11.0+ ARM64

File details

Details for the file atomicx-0.0.15.tar.gz.

File metadata

  • Download URL: atomicx-0.0.15.tar.gz
  • Upload date:
  • Size: 15.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.9.4

File hashes

Hashes for atomicx-0.0.15.tar.gz
Algorithm Hash digest
SHA256 6e1f90e569e0ed61109907fb05689c67ba95c97e99496773c90040e10826b035
MD5 60e03a3ff8edfb30be45e1bcaa5ff323
BLAKE2b-256 2c2d9db45a9d9bb294239575730b31ac3e65de2d987364f27d9ec05215af7c49

See more details on using hashes here.

File details

Details for the file atomicx-0.0.15-cp313-cp313t-win_amd64.whl.

File metadata

File hashes

Hashes for atomicx-0.0.15-cp313-cp313t-win_amd64.whl
Algorithm Hash digest
SHA256 5f891d06ab424366ec2f8aef3c697715aabd443b83955d8d427ebdbb123b2900
MD5 335f49f29213e316e449b1364c3b4e92
BLAKE2b-256 480e8680ccb16f9c808cafa35fb737de78052d2267cd2418f76d8e88760f1a59

See more details on using hashes here.

File details

Details for the file atomicx-0.0.15-cp313-cp313t-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for atomicx-0.0.15-cp313-cp313t-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 d3e9d9edf09bc0ad4e093d448df00cc3cee9312698331664eb1752b86b507776
MD5 f1a4862e546459c5b8acecf93004c051
BLAKE2b-256 f58ecc60709adde38969d590f7b61585cf474434db9c46faae29a2b009a4d22e

See more details on using hashes here.

File details

Details for the file atomicx-0.0.15-cp313-cp313t-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl.

File metadata

File hashes

Hashes for atomicx-0.0.15-cp313-cp313t-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 2a21f183742635275a3aeb2aec014f58f7dd18e96d20bd5f582113dcd305e6eb
MD5 f796f14f2effa9139c6cdf78e0987500
BLAKE2b-256 cf339950b57d43c5d2b4168e9ed3443d6a6252ed7f51fed1dbf14b20956d3f16

See more details on using hashes here.

File details

Details for the file atomicx-0.0.15-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for atomicx-0.0.15-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 562d4de7183529065ee8ea82046eeb2b4bbddeb7771b8b87658e934143c34f50
MD5 52aa1ac85f6a379ad779727c81e60122
BLAKE2b-256 ecacd783bbe3ff3a1c0c730204ab33541c3c486270afb0c8a14bfd9479ad3f74

See more details on using hashes here.

File details

Details for the file atomicx-0.0.15-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for atomicx-0.0.15-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 15354cff1fee57ffc183f90cd3b51e62b1cc391ec16acc288128eed9f51d8865
MD5 541c43f665ff86ff8cddea5f81ec3d4b
BLAKE2b-256 4b38c2e84de8ccc869c199b12054f6b08788dbf87762cb480b5263c79881e30a

See more details on using hashes here.

File details

Details for the file atomicx-0.0.15-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for atomicx-0.0.15-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 ae128e880fbc6d85c73e46f666bb39b8e103f7a528006e60b16833a3690df732
MD5 6e066755d43ba55e6e0eaf699db1299d
BLAKE2b-256 1dd345e6e6046c28237f041269397bccbea45eb2d6933e5750196e8d95e3b909

See more details on using hashes here.

File details

Details for the file atomicx-0.0.15-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: atomicx-0.0.15-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 156.2 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.9.4

File hashes

Hashes for atomicx-0.0.15-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 695a4fbc93ea5b6dd5d5b9a19a0406156fb9c698c733667a04d748248c97089c
MD5 bb7fbe757d50c3149a75ea4706045a42
BLAKE2b-256 d89820cf5bcca4d684a2baf17117ab4db58049f99e8b30ec8f201cae8f0d48ac

See more details on using hashes here.

File details

Details for the file atomicx-0.0.15-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: atomicx-0.0.15-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 158.8 kB
  • Tags: CPython 3.8+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.9.4

File hashes

Hashes for atomicx-0.0.15-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 32ce46bcdc328b12780084773536ecedffa6fecb1ad4cf30b839d694d2e9990e
MD5 94f60999e80e7639abfc1bd27c6c9d13
BLAKE2b-256 4b305b6c95a505e4da794ae5095de67f397742fc5d51729d4ff2f457920a59b9

See more details on using hashes here.

File details

Details for the file atomicx-0.0.15-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for atomicx-0.0.15-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 59d5cc5c32756d0429dff20abb2fc1f5eeb3efaa55e73f081ec6b868c542b7e9
MD5 abc055b6ba4ece6d6f6bf8ed7fb44bcb
BLAKE2b-256 4df0627c353a6945e481c6d9eb57637464b1f7a61962d4b2ea4c97a8f59bc25f

See more details on using hashes here.

File details

Details for the file atomicx-0.0.15-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl.

File metadata

File hashes

Hashes for atomicx-0.0.15-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 07fecd0ca8e177c3d1bab3b4b61f802eeb1336dae952af1f28e21eac090d9268
MD5 9dc7c50f9c7a108f6bac930ebd7bc589
BLAKE2b-256 a4459de50e8f33d880ae83b6d6909169de4de72ee43623a7b740e5d3bae1cc64

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