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.15rc1.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.15rc1-cp313-cp313t-win_amd64.whl (156.0 kB view details)

Uploaded CPython 3.13tWindows x86-64

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

Uploaded CPython 3.13tmanylinux: glibc 2.34+ x86-64

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

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

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.10Windows x86-64

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

Uploaded CPython 3.9Windows x86-64

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

Uploaded CPython 3.8+Windows x86-64

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

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

atomicx-0.0.15rc1-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (536.9 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.15rc1.tar.gz.

File metadata

  • Download URL: atomicx-0.0.15rc1.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.15rc1.tar.gz
Algorithm Hash digest
SHA256 a827d080905a03a9055269d031af06f5bf692df19da307f04be7db62faba87b3
MD5 8f9f549087ec132438f3baa02a6574a8
BLAKE2b-256 1ee3d5814470b45de0c97f5198ea820087901a39b2658940291359b09b8c7bfa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for atomicx-0.0.15rc1-cp313-cp313t-win_amd64.whl
Algorithm Hash digest
SHA256 2863c3ae259e3b8e2e7206bd1638c7c66e8da7b3dd6176ca5b16d5be7759cce8
MD5 b485c1ab30feb6c816b476e55a351439
BLAKE2b-256 e83defc57869b5a31bbae14b1aaabbd22bca5a22103593d6de359c71f5eae760

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for atomicx-0.0.15rc1-cp313-cp313t-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 55fcaf65d29e927260ea4710b795d981b14b418e2ebb2b5da40d40cc4eeebc0e
MD5 7033086f056fc398207862ebe3544761
BLAKE2b-256 350efb7767e5cd4bc72f99c0b074a2614310bc5eeb0f657956befddee5294cac

See more details on using hashes here.

File details

Details for the file atomicx-0.0.15rc1-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.15rc1-cp313-cp313t-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 cb5686193a9ffa36e6f73f04b1e3c247714ec4af6ff6eea3915eccc5bb5a12ee
MD5 320845bbaa5d5c380a21c57eee1070fa
BLAKE2b-256 e3589788cdfc74f2c8a5f2373e79d57dbbea0b7a3d287e7ab6f4d8fd63125110

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for atomicx-0.0.15rc1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 0ab9c175d61c3cf149c47459e10c7c57315d2eb0006aaec90bdbafe0a8073cee
MD5 b72f8d63278654c52cb9b784b8dd1821
BLAKE2b-256 f6835d808967dcadb442beaa5ddead1ea240aebf8a453e50a22d3b520c29e5d4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for atomicx-0.0.15rc1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 b18c41b2a9500343f27e3024e422e247b298010ebdc873d1f12a7bd04bc62d49
MD5 50127bb9b4732c2d00bec7bd6880de32
BLAKE2b-256 47107366f1b015d7b3d5b86683c7ff2ca10a0ae82851d84fe2570b10e6b520eb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for atomicx-0.0.15rc1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 9696d1667980e9d948c214ff54ab11dc114d62ab14393d9c7fdd63fd5852966d
MD5 fbac8f55aefc85b6b92ce384f33c2ecb
BLAKE2b-256 9aeba8b0bc1332c8c5795f33f9fc0fcf1bf76c722c801fc5449c7d163a1870ad

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for atomicx-0.0.15rc1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 ec61ffba55115f1cf2450e6327c52ea7a3ea55b63db23e6e3272a6ae2be31eec
MD5 bc12fdbf1347c9c0e4f507178741f2e1
BLAKE2b-256 8de18c82dff0825871b68e33eab07e00f2d5b9784413cecf8509f6fbfcf9ff5e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for atomicx-0.0.15rc1-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 0e296b2f40d90c4ad4c7766d6ca1e211877994315acaafb711efbae9647cf620
MD5 6013d3db7423610aefb3b96bdc682cbe
BLAKE2b-256 e5c3d4cc6818c9f59cf64262fcd35457eb5002c29f90836d69177385b4ebe2b6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for atomicx-0.0.15rc1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e4604fd1e43b5f180aa14aa109a47f4bc3eefe9fef273da383606d8d0e63c3cf
MD5 b8646212aad837e672105637def9a774
BLAKE2b-256 66b4ec3d7f57fc4cb153710b9b403f60eaba64c7933146912ea26ad3bc472316

See more details on using hashes here.

File details

Details for the file atomicx-0.0.15rc1-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.15rc1-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 cc343058fafffe6b3a9150679cdaf656eba32906cdac4fd69c3cf1368547a261
MD5 94c340bdb29e2f5fa5f0dbe723e74b26
BLAKE2b-256 d9aab2c6df9b370b4843e9ea0b2d24cac21691a2334c75b90a12633242cdfb88

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