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}")

# 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()

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.15rc0.tar.gz (15.1 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.15rc0-cp313-cp313t-win_amd64.whl (154.0 kB view details)

Uploaded CPython 3.13tWindows x86-64

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

Uploaded CPython 3.13tmanylinux: glibc 2.34+ x86-64

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

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

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.10Windows x86-64

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

Uploaded CPython 3.9Windows x86-64

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

Uploaded CPython 3.8+Windows x86-64

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

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

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

File metadata

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

File hashes

Hashes for atomicx-0.0.15rc0.tar.gz
Algorithm Hash digest
SHA256 3c7f76d7a13f571b119eadee1e784322eeecb3f338c9fc6fffabadab40c706c3
MD5 75c8de94156896c8d239d0489ef1d6d7
BLAKE2b-256 1a29ed57f385e40cb778786efaaf9312b774c8dc3df549c964ac3dde99dbbbb9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for atomicx-0.0.15rc0-cp313-cp313t-win_amd64.whl
Algorithm Hash digest
SHA256 777fad926d56350eeb65a0d7d7f6122601bbe8e21a2de4463dfefbaf0015093c
MD5 5670eec9eb28a83a127bda85751ed57d
BLAKE2b-256 3a9aafdadbb568dce1bb6182f48e8ef0f75619f4fb06ca67d894e674a16d6795

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for atomicx-0.0.15rc0-cp313-cp313t-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 bec506f2bc2c5330994fa571c6d6d7df89e335f67657474cd9cfdc135aaf814e
MD5 a6f6e561959838022e400cf376f90f10
BLAKE2b-256 46cd8ba9e0c0767cf3b2c31f9c532acdb2d5d127fb8bf445cd4e82196b0aa5e7

See more details on using hashes here.

File details

Details for the file atomicx-0.0.15rc0-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.15rc0-cp313-cp313t-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 0b809d6390f6d3e0641d9e328957d47abe220678469c2d026ded2721f22b37a5
MD5 7cdb7331602955b36c96476f370656e4
BLAKE2b-256 7ba571c11944fbe9bf33611ff38674a4165fbfda2279c6fea1c08fa076057c48

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for atomicx-0.0.15rc0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 9574ca78aa4d79b461c4b6d9ca58c9fa499c97cd655a2e3cd472c754a95fa04b
MD5 01a0572fd328fe25ac15297963e5052d
BLAKE2b-256 221916beb4e31b507e46606d33b662a1b6f63e06866d3e8205e1bd27f32a8dee

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for atomicx-0.0.15rc0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 3a247627448fc35cd48040a68b06640199b70c0b579e51922e50bb0661f22d35
MD5 b99aa208da754c4abf6e1f2261a8ee79
BLAKE2b-256 ad98970768600ba187ab7d334d76c2b06ae4581c0816ad29ca2af67f505c76fb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for atomicx-0.0.15rc0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 200e33a4889915707ef8b4e2f4124247261e1e0c350147f3bbd98b7bd5c20d82
MD5 7e6c6b1835c274457285c3e00532c893
BLAKE2b-256 c2d0c7fc2e5e72da890ab7ef602b6750178a0586c26c2546624e64ce6af32dab

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for atomicx-0.0.15rc0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 301e60c1dc6334f4263595e7ed71b1545ec76753216da41ed6519003a2bf199d
MD5 0fbfb240d072acf5100cacc78def449f
BLAKE2b-256 1cb64a94ee4a01f55048d52a20dc22d927e3703dd28ed499480a1dcab09be02e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for atomicx-0.0.15rc0-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 57a0ac782c417586087dbd27cb4c65cc8e262ef03a88bd133af7655018045d34
MD5 ff547e3c12c4cfabe86f1e8d9abb596c
BLAKE2b-256 fd9a2af0c25c839f88d41b2c3f2be3c67637c041d03c8a0771ba6a8722e54377

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for atomicx-0.0.15rc0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 624bff2757bbb4796207aab4ff72457528e2f28acb8ec4df7ebce1676f1cdfb1
MD5 1a9150c4af8dc300718aed8a726b18c7
BLAKE2b-256 636db504a28ec28d188745c5a325105c3b562010fd6a7d9ad5294c615a899bec

See more details on using hashes here.

File details

Details for the file atomicx-0.0.15rc0-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.15rc0-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 716d49242042388fb3495dee7480090a8ccd274bc96184a279466870be096519
MD5 62091f9349771cf2f181c1054e70e8cc
BLAKE2b-256 1558e331441f3d8856a18ca6b79a67363a1823cb7d94468ffe87a84030fa8140

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