Skip to main content

Cross platform atomic int for Python

Project description

pyatomix provides std::atomic_flag and std::atomic<int64_t> for Python

Compatible with Python 3.13 free-threaded. Windows users will need to have Visual Studio (or VS Build Tools) installed to build this. It uses Pybind11 and pip/setuptools will build it automatically. It's been tested on Windows and Linux.

Linux users will need the Python dev package installed for their Python version to install atomix, for instance: python3.13-dev

Installation

pip install -U pyatomix
-or-
git clone https://github.com/0xDEADFED5/pyatomix.git
pip install -U ./pyatomix

Usage

from pyatomix import AtomicInt, AtomicFlag
x = AtomicInt(7)     # initial value of 7
x += 1               # now it's 8, this is atomic
y = AtomicFlag()     # y == False
z = y.test_and_set() # z == False, y == True
y.clear()            # y == False

the API is exactly the same as std::atomic and std::atomic_flag. all the math operators except **, >>, <<=, >>, >>= are overridden for AtomicInt.

atomic assignment operators: +=, -=, &=, |=, ^= (using these are an atomic operation)

other assignment operators like %=,/=,//=,*= first load the value, then store the modified value. those operators are thread-safe, but not atomic.

Performance

Depending on compiler and OS, AtomicInt increment is 4-6x slower than a standard increment, which is still pretty fast. 1 million atomic increments in a for loop takes me 160ms in Linux, while incrementing a regular int 1 million times takes 40ms.

To run the tests

from pyatomix import AtomicTest
x = AtomicTest()
x.run()

NOTE: the AtomicFlag test will hang unless you're running a free-threaded build of Python

API list, all these are atomic

AtomicInt.is_lock_free() -> bool   : returns True if it's lock free, should be True for most
AtomicInt.store(value) -> None     : assign value, doesn't return anything
AtomicInt.load() -> int            : read value
AtomicInt.fetch_add(value) -> int  : add to current value, return previous value
AtomicInt.fetch_sub(value) -> int  : subtract from current value, return previous value
AtomicInt.exchange(value) -> int   : assign value, return previous value
AtomicInt.compare_exchange(expected, value) -> bool : 
    if current value == expected, replace it with value. returns True on success.
AtomicInt.compare_exchange_weak(expected, value) -> bool : 
    same as compare_exchange, but may fail spuriously. faster on some platforms, use in a loop.

AtomicFlag.clear() -> None        : set value to False
AtomicFlag.test_and_set() -> bool : set value to True and return previous value
AtomicFlag.test() -> bool         : read value
AtomicFlag.wait(bool cmp) -> bool : if current value == cmp, wait until signaled
AtomicFlag.notify_one() -> None   : notify one or more threads that the value has changed
AtomicFlag.notify_all() -> None   : notify all threads that the value has changed

AtomicFlag overloaded operators:

==,!= (will work with "falsy" types too)

AtomicInt overloaded operators:

==,!=,-,~,+,+=,-=,*,*=,/,/=,//,//=,|,|=,%,%=,&,&=,^,^=,>,>=,<,<=

(trying to use anything other than an int with these will fail)

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

pyatomix-0.1.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

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

pyatomix-0.1.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file pyatomix-0.1.0.tar.gz.

File metadata

  • Download URL: pyatomix-0.1.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1+

File hashes

Hashes for pyatomix-0.1.0.tar.gz
Algorithm Hash digest
SHA256 906585c47fa26f0fd8ccb486f1b936a7daed0d0edfb4918488402637ae4bb41d
MD5 7ef0a08a9e89dbb83d7a8d69610a3334
BLAKE2b-256 abd1edc40e6b7bb3b4da331571189c3cb6a8c980ac31ccdf4da9eb463423bce0

See more details on using hashes here.

File details

Details for the file pyatomix-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pyatomix-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1+

File hashes

Hashes for pyatomix-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 989412ed4582933cf8a8eb1e0be38e0677d39683a3d323e9d88e7acd8f6bdd86
MD5 0b6843c02f4676b1e106c64d8d58a8a6
BLAKE2b-256 8cc6cc2c99b6fc3b64dd2aa37d70945596a000ecd854fe4352c11c699bbda08f

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