Skip to main content

PyZas!: A Python library for atomic integer operations

PyZas is a Python library designed for performing atomic integer operations with the speed and efficiency of a zas! (Inspired by the sudden impact sound 'zas', a Spanish onomatopoeia). PyZas ensures your calculations are executed atomically and safely. Perfect for scenarios requiring quick, thread-safe manipulations.

Installation

You can install PyZas via pip:

pip install pyzas

Usage

The PyZas module implements the following atomic types:

  • AtomicInt: An atomic int type
  • AtomicLong: An atomic 64-bytes int type
  • AtomicULong: An atomic 64-bytes unsigned int type

The three types implement the following functions:

  • free_lock_level() -> int: Determines if the atomic object is implemented lock-free
  • load() -> int: Reads a value from an atomic object
  • store(int): Stores a value in an atomic object
  • exchange(int) -> int: Atomically replaces the value in an atomic object
  • fetch_add(int) -> int: Performs atomic addition
  • fetch_sub(int) -> int: Performs atomic subtraction
  • compare_exchange(int, int) -> int: Swaps a value with an atomic object if the old value is what is expected, otherwise reads the old value

Module also implements AtomicFlag with support for spinlock:

  • test_and_set() -> bool: sets an atomic flag to true and returns the old value
  • clear(): Sets the atomic_flag to false
  • spin_lock(): Causes a thread trying to acquire a lock to simply wait in a loop ("spin")
  • spin_unlock(): Releases the lock

For more details:

help(pyzas)

Example

import pyzas

n = pyzas.AtomicInt(0)
n.fetch_add(1)
print(n.load())

Performance Test

from multiprocessing.pool import ThreadPool
import threading
import time
import sys

import pyzas.pyzas as pyzas


def main(threads, its):
    e = 0
    atomic_e = pyzas.AtomicInt()
    lock = threading.Lock()

    def test_lock(i):
        nonlocal e
        with lock:
            e = e + 1

    def test_atomic(i):
        atomic_e.fetch_add(1)

    start = time.time()
    with ThreadPool(threads) as p:
        list(p.map(test_lock, range(its)))
    print("Testing with lock", e, time.time() - start)

    start = time.time()
    with ThreadPool(threads) as p:
        list(p.map(test_atomic, range(its)))
    print("Testing atomic   ", atomic_e.load(), time.time() - start)


if __name__ == '__main__':
    if len(sys.argv) < 3:
        print(f"Use: python3 {sys.argv[0]} [threads] [its]", file=sys.stderr)
        exit(-1)
    main(int(sys.argv[1]), int(sys.argv[2]))

Build from sources

Library is built using Poetry:

poetry build

Or to install the library with pip:

pip install .

PYZAS_CFLAGS environment variable can be used to add compilation options.

export PYZAS_CFLAGS="-O2"
poetry build

Most common errors

- Visual Studio still has experimental support for atomics, so we need to enable it to compile the code.

SET PYZAS_CFLAGS=/std:c11 /experimental:c11atomics
poetry build # or pip install .

- cp313 version (with GIL) is being compiled instead of the cp313t version (free-threaded)

If your version has been compiled with GIL, even if you have the free-threaded binaries, it's possible that the build is done for the GIL version. The build tools are still experimental, and don't have a way to fix the compile option, so we have to do it manually.

export PYZAS_FIX_GIL=1 # Linux or MacOS
SET PYZAS_FIX_GIL=1 # Windows

If you want to install using the wheel, you'll need to add t after the version number. For example, pyzas-1.0-cp313-cp313-win_amd64.whl should be pyzas-1.0-cp313-cp313t-win_amd64.whl.

Metadata

Release files for pyzas 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyzas 1.0.1
File Size Uploaded Provenance
pyzas-1.0.1.tar.gz 15.1 kB Without provenance Details

Built distributions (wheels)

Table of built distributions (wheels) for pyzas 1.0.1
File
pyzas-1.0.1-cp313-cp313t-win_amd64.whl CPython 3.13 CPython 3.13 free-threading Windows x86-64 Details
pyzas-1.0.1-cp313-cp313t-manylinux_2_31_x86_64.whl CPython 3.13 CPython 3.13 free-threading Linux glibc 2.31+ x86-64 Details
pyzas-1.0.1-cp313-cp313t-manylinux_2_31_aarch64.whl CPython 3.13 CPython 3.13 free-threading Linux glibc 2.31+ ARM64 Details
pyzas-1.0.1-cp313-cp313t-macosx_13_0_x86_64.whl CPython 3.13 CPython 3.13 free-threading macOS 13.0+ x86-64 Details

Total release size: 139.4 kB

Release files / pyzas-1.0.1.tar.gz

Download URL pyzas-1.0.1.tar.gz
Size 15.1 kB
Tags Source
SHA-256 checksum
How to use checksums
9d500dd1e7007489bcfe3d90c13da25b15f85cab79b699d137d91f6a88a10697
BLAKE2b-256 checksum
How to use checksums
b3c3ccf0f2adc3003f7962841b250afd65bf2f087aabeebfc8bd100646714f09
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.0 CPython/3.10.4 Windows/10

Release files / pyzas-1.0.1-cp313-cp313t-win_amd64.whl

Download URL pyzas-1.0.1-cp313-cp313t-win_amd64.whl
Size 22.5 kB
Tags CPython 3.13 CPython 3.13 free-threading Windows x86-64
SHA-256 checksum
How to use checksums
b1e63868735e4752107f73843c602acb7d7996cc02626eb3e9717bd106990b1e
BLAKE2b-256 checksum
How to use checksums
a64b30f557832ec8aac7eecef8aa3b91f60ad99f6883a54d9b2bcb1d656158f8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.0 CPython/3.10.4 Windows/10

Release files / pyzas-1.0.1-cp313-cp313t-manylinux_2_31_x86_64.whl

Download URL pyzas-1.0.1-cp313-cp313t-manylinux_2_31_x86_64.whl
Size 40.7 kB
Tags CPython 3.13 CPython 3.13 free-threading Linux glibc 2.31+ x86-64
SHA-256 checksum
How to use checksums
e14b4b0ba14d567c3579d82c6ad18db156f4d571a202a53c120cde04d7fac258
BLAKE2b-256 checksum
How to use checksums
3743f50df0c85aa8a371e8256818d933ef23a7dd4be3ecaec74a863188b5b7b1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.0 CPython/3.10.4 Windows/10

Release files / pyzas-1.0.1-cp313-cp313t-manylinux_2_31_aarch64.whl

Download URL pyzas-1.0.1-cp313-cp313t-manylinux_2_31_aarch64.whl
Size 40.9 kB
Tags CPython 3.13 CPython 3.13 free-threading Linux glibc 2.31+ ARM64
SHA-256 checksum
How to use checksums
a5e125052c534d72904bc837cfda9dc1d1f06bc4e782e62d21ea3d079e39dbae
BLAKE2b-256 checksum
How to use checksums
f9c787433383aca619652c6770fefad906ba879e8bd43351294c652da6cf3b2c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.0 CPython/3.10.4 Windows/10

Release files / pyzas-1.0.1-cp313-cp313t-macosx_13_0_x86_64.whl

Download URL pyzas-1.0.1-cp313-cp313t-macosx_13_0_x86_64.whl
Size 20.2 kB
Tags CPython 3.13 CPython 3.13 free-threading macOS 13.0+ x86-64
SHA-256 checksum
How to use checksums
f836b9b70a3d197aa5f3fb9b9f9c8f80bb266a08bab87aab4e04fbe8f44b6e44
BLAKE2b-256 checksum
How to use checksums
80cf60efaa772c293129306b311d392445269195422cbd1398a79a8e1147b642
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.0 CPython/3.10.4 Windows/10

Security for pyzas 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.1 This release

5 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page