Skip to main content

easy-to-use lock-free atomic integers and booleans for multithreading

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.

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.7 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.13.tar.gz (13.3 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.13-cp37-abi3-win_amd64.whl (158.7 kB view details)

Uploaded CPython 3.7+Windows x86-64

atomicx-0.0.13-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (291.9 kB view details)

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

atomicx-0.0.13-cp37-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (505.9 kB view details)

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

File details

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

File metadata

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

File hashes

Hashes for atomicx-0.0.13.tar.gz
Algorithm Hash digest
SHA256 4b7d5b716fb8ce1bfd18a48e03d38a7468f77b68001b8c25096973bcfcc14b54
MD5 5e372a86218bf2f3f3abbb78c759c552
BLAKE2b-256 0263de1859a3d74cf4cbbacfa3477bbffc59ca393d48a182855e60295772f199

See more details on using hashes here.

File details

Details for the file atomicx-0.0.13-cp37-abi3-win_amd64.whl.

File metadata

  • Download URL: atomicx-0.0.13-cp37-abi3-win_amd64.whl
  • Upload date:
  • Size: 158.7 kB
  • Tags: CPython 3.7+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.7.4

File hashes

Hashes for atomicx-0.0.13-cp37-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 80aed62f88e84a57872d9110f2b2f525d58b0bad2ecf1ec85cde372d675b6c06
MD5 bf4ccba06349617d15428ba2c3891623
BLAKE2b-256 5ebf7739fccd7febe2cf2262e6a6477b6b499f0c72387c6f8c8645597a298815

See more details on using hashes here.

File details

Details for the file atomicx-0.0.13-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for atomicx-0.0.13-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 289fd8a162a643e2ffb42e791abe04166297c5072215b60fbc356f8c2ce29089
MD5 e36a56d9baa4d561ee5e459d79580b52
BLAKE2b-256 f7ae29b58722d74147f6ab21ee445154cd89e1565379fc9116730c9987ac3034

See more details on using hashes here.

File details

Details for the file atomicx-0.0.13-cp37-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl.

File metadata

File hashes

Hashes for atomicx-0.0.13-cp37-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 6e8cf95a05371db8eafd6f171a1793b4eb691a5ccc287fa198f7025cd23d0dae
MD5 12c18d00e8df8048fc61c8738e900e41
BLAKE2b-256 1143ffb8efc5ec056ff8db4bbc66d2fcf673a614b30e0d9351e2561462c63b6e

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