{"id":22155894,"url":"https://github.com/citiususc/pyzas","last_synced_at":"2026-01-16T01:41:42.635Z","repository":{"id":243144709,"uuid":"811495046","full_name":"citiususc/pyzas","owner":"citiususc","description":"Performing Python atomic integer operations with the speed and efficiency of a zas!","archived":false,"fork":false,"pushed_at":"2024-06-10T12:02:03.000Z","size":26,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-10T02:51:55.471Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/citiususc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-06T17:50:56.000Z","updated_at":"2024-06-10T12:01:19.000Z","dependencies_parsed_at":"2024-06-07T00:34:01.853Z","dependency_job_id":"de9b2045-ac40-4cd2-9cc6-a6e4c77f69ff","html_url":"https://github.com/citiususc/pyzas","commit_stats":null,"previous_names":["citiususc/pyzas"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/citiususc%2Fpyzas","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/citiususc%2Fpyzas/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/citiususc%2Fpyzas/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/citiususc%2Fpyzas/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/citiususc","download_url":"https://codeload.github.com/citiususc/pyzas/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246911467,"owners_count":20853657,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-02T02:32:47.685Z","updated_at":"2026-01-16T01:41:42.610Z","avatar_url":"https://github.com/citiususc.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PyZas!: A Python library for atomic integer operations\n\n**PyZas** is a Python library designed for performing atomic integer operations with the speed and efficiency of a zas!\n(Inspired by the sudden impact sound 'zas', a Spanish onomatopoeia). PyZas ensures your calculations are executed atomically and safely. Perfect \nfor scenarios requiring quick, thread-safe manipulations.\n\n## Installation\n\nYou can install PyZas via pip:\n\n```bash\npip install pyzas\n```\n\n## Usage\n\nThe PyZas module implements the following atomic types:\n\n* **AtomicInt**: An atomic int type\n* **AtomicLong**:  An atomic 64-bytes int type\n* **AtomicULong**: An atomic 64-bytes unsigned int type\n\nThe three types implement the following functions:\n\n* **free_lock_level() -\u003e int**: Determines if the atomic object is implemented lock-free\n* **load() -\u003e int**: Reads a value from an atomic object\n* **store(int)**: Stores a value in an atomic object\n* **exchange(int) -\u003e int**: Atomically replaces the value in an atomic object\n* **fetch_add(int) -\u003e int**: Performs atomic addition\n* **fetch_sub(int) -\u003e int**: Performs atomic subtraction\n* **compare_exchange(int, int) -\u003e int**: Swaps a value with an atomic object if the old value is what is expected, otherwise reads the old value\n\nModule also implements **AtomicFlag** with support for spinlock:\n\n* **test_and_set() -\u003e bool**: sets an atomic flag to true and returns the old value\n* **clear()**: Sets the atomic_flag to false\n* **spin_lock()**: Causes a thread trying to acquire a lock to simply wait in a loop (\"spin\")\n* **spin_unlock()**: Releases the lock\n\nFor more details:\n\n```python\nhelp(pyzas)\n```\n\n## Example\n\n```python\nimport pyzas\n\nn = pyzas.AtomicInt(0)\nn.fetch_add(1)\nprint(n.load())\n```\n\n## Performance Test\n\n```python\nfrom multiprocessing.pool import ThreadPool\nimport threading\nimport time\nimport sys\n\nimport pyzas.pyzas as pyzas\n\n\ndef main(threads, its):\n    e = 0\n    atomic_e = pyzas.AtomicInt()\n    lock = threading.Lock()\n\n    def test_lock(i):\n        nonlocal e\n        with lock:\n            e = e + 1\n\n    def test_atomic(i):\n        atomic_e.fetch_add(1)\n\n    start = time.time()\n    with ThreadPool(threads) as p:\n        list(p.map(test_lock, range(its)))\n    print(\"Testing with lock\", e, time.time() - start)\n\n    start = time.time()\n    with ThreadPool(threads) as p:\n        list(p.map(test_atomic, range(its)))\n    print(\"Testing atomic   \", atomic_e.load(), time.time() - start)\n\n\nif __name__ == '__main__':\n    if len(sys.argv) \u003c 3:\n        print(f\"Use: python3 {sys.argv[0]} [threads] [its]\", file=sys.stderr)\n        exit(-1)\n    main(int(sys.argv[1]), int(sys.argv[2]))\n```\n\n## Build from sources\n\nLibrary is built using Poetry:\n\n```bash\npoetry build\n```\n\nOr to install the library with pip:\n\n```bash\npip install .\n```\n\n*PYZAS_CFLAGS* environment variable can be used to add compilation options. \n\n```bash\nexport PYZAS_CFLAGS=\"-O2\"\npoetry build\n```\n\n### Most common errors\n\n#### - Visual Studio still has experimental support for atomics, so we need to enable it to compile the code.\n\n```bash\nSET PYZAS_CFLAGS=/std:c11 /experimental:c11atomics\npoetry build # or pip install .\n```\n\n#### - cp313 version (with GIL) is being compiled instead of the cp313t version (free-threaded)\n\nIf your version has been compiled with GIL, even if you have the free-threaded binaries, it's possible that the build \nis done for the GIL version. The build tools are still experimental, and don't have a way to fix the compile  option, \nso we have to do it manually.\n\n```bash\nexport PYZAS_FIX_GIL=1 # Linux or MacOS\nSET PYZAS_FIX_GIL=1 # Windows\n```\n\nIf you want to install using the wheel, you'll need to add **t** after the version number. For example, \n*pyzas-1.0-cp313-cp313-win_amd64.whl* should be *pyzas-1.0-cp313-cp313t-win_amd64.whl*.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcitiususc%2Fpyzas","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcitiususc%2Fpyzas","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcitiususc%2Fpyzas/lists"}