Skip to main content

GIL-powered* locking library for Python

Project description

aiologic is a locking library for tasks synchronization and their communication. It provides primitives that are both async-aware and thread-aware, and can be used for interaction between:

  • async codes (async <-> async) in one thread as regular async primitives

  • async codes (async <-> async) in multiple threads (!)

  • async code and sync one (async <-> sync) in one thread (!)

  • async code and sync one (async <-> sync) in multiple threads (!)

  • sync codes (sync <-> sync) in one thread as regular sync primitives

  • sync codes (sync <-> sync) in multiple threads as regular sync primitives

Let’s take a look at the example:

import asyncio

from threading import Thread

import aiologic

lock = aiologic.Lock()


async def func(i: int, j: int) -> None:
    print(f"thread={i} task={j} start")

    async with lock:
        await asyncio.sleep(1)

    print(f"thread={i} task={j} end")


async def main(i: int) -> None:
    await asyncio.gather(func(i, 0), func(i, 1))


Thread(target=asyncio.run, args=[main(0)]).start()
Thread(target=asyncio.run, args=[main(1)]).start()

It prints something like this:

thread=0 task=0 start
thread=1 task=0 start
thread=0 task=1 start
thread=1 task=1 start
thread=0 task=0 end
thread=1 task=0 end
thread=0 task=1 end
thread=1 task=1 end

As you can see, tasks from different event loops are all able to acquire aiologic.Lock. In the same case if you use asyncio.Lock, it will raise a RuntimeError. And threading.Lock will cause a deadlock.

Features

  • Python 3.8+ support

  • CPython and PyPy support

  • Experimental Nuitka support

  • Pickling and weakrefing support

  • Cancellation and timeouts support

  • Optional Trio-style checkpoints:

    • enabled by default for Trio itself

    • disabled by default for all others

  • Only one checkpoint per asynchronous call:

    • exactly one context switch if checkpoints are enabled

    • zero or one context switch if checkpoints are disabled

  • Fairness wherever possible (with some caveats)

  • Thread-safety wherever possible

  • Lock-free implementation (with some exceptions)

  • Bundled stub files

Synchronization primitives:

  • Events: one-time, reusable, and countdown

  • Barriers: single-use, cyclic, and reusable

  • Semaphores: counting, bounded, and binary

  • Capacity limiters: borrowable, and reentrant

  • Locks: ownable, and reentrant

  • Readers-writer locks (external)

  • Condition variables

Communication primitives:

  • Queues: FIFO, LIFO, and priority

Non-blocking primitives:

Supported concurrency libraries:

All synchronization, communication, and non-blocking primitives are implemented entirely on effectively atomic operations, which gives an incredible speedup on PyPy compared to alternatives from the threading module. All this works because of GIL, but per-object locks also ensure that the same operations are still atomic, so aiologic also works when running in a free-threaded mode.

Installation

Install from PyPI (stable):

pip install aiologic

Or from GitHub (latest):

pip install git+https://github.com/x42005e1f/aiologic.git

You can also use other package managers, such as uv.

Documentation

Read the Docs: https://aiologic.readthedocs.io (official)

DeepWiki: https://deepwiki.com/x42005e1f/aiologic (AI generated; lying!)

There are also related posts:

Communication channels

GitHub Discussions: https://github.com/x42005e1f/aiologic/discussions (ideas, questions)

GitHub Issues: https://github.com/x42005e1f/aiologic/issues (bug tracker)

You can also send an email to 0x42005e1f@gmail.com with any feedback.

Project status

The project is developed and maintained by one person in his spare time and is not a commercial product. The author is not a professional programmer, so you may encounter some misunderstandings. However, he has been programming as a hobby for over a decade, delving into specific topics (often esoteric ones), and until 2024, he made almost no public contributions (you can find some if you try hard; for example, one very ugly one in Java from 2019), as he set high standards for himself. The author often makes mistakes, so he constantly double-checks and improves himself (which is well reflected in how often he edits his own comments) — as a result, he relies heavily on careful theoretical analysis and proactive bug fixing.

No AI tools are used in the development (nor are IDE tools, for that matter). The only exception is text translation, since the author is not a native English speaker, but the texts themselves are not generated (they are written and edited manually by a human). Unicode characters are also actively used (via a compose key), as the author likes beautiful texts, such as they were before the avoidance of signs of AI writing. — It may not be too obvious, but this paragraph addresses the sore subject of “you are an AI”, expressed in… not the best ways.

It is published for the simple reason that the author considered it noteworthy and not too ugly. The topic is quite non-trivial, so although contributions are not prohibited, they will be very, very difficult if you decide to make them (except for some very simple ones). The functionality provided is still being perfected, so the development status is alpha.

What is the goal of the project? To realize the author’s vision. Is it worth trusting what is available now? Well, the choice is yours. But the project is already being used, so why not give it a try?

License

The aiologic library is REUSE-compliant and is offered under multiple licenses:

  • All original source code is licensed under ISC.

  • All original test code is licensed under 0BSD.

  • All documentation is licensed under CC-BY-4.0.

  • All configuration is licensed under CC0-1.0.

For more accurate information, check the individual files.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiologic-0.17.0.tar.gz (251.6 kB view details)

Uploaded Source

Built Distribution

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

aiologic-0.17.0-py3-none-any.whl (161.1 kB view details)

Uploaded Python 3

File details

Details for the file aiologic-0.17.0.tar.gz.

File metadata

  • Download URL: aiologic-0.17.0.tar.gz
  • Upload date:
  • Size: 251.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for aiologic-0.17.0.tar.gz
Algorithm Hash digest
SHA256 65aa058e858c94cd208badb188e7f00b54dcabb3ba85b34f794db98074d108b9
MD5 3d13a72beba935bfd9a7b63ae3771364
BLAKE2b-256 53a7809482759f40079f4c4328c7318bf569ae25d457f5017aad30a1b9aafedc

See more details on using hashes here.

File details

Details for the file aiologic-0.17.0-py3-none-any.whl.

File metadata

  • Download URL: aiologic-0.17.0-py3-none-any.whl
  • Upload date:
  • Size: 161.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for aiologic-0.17.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1bf4d3e4314df2bcb06a9e696417204e206ab50e10ec98d28d157e2e57634f74
MD5 1a68559c19b1a3f47c544d5a99771be4
BLAKE2b-256 9e6b5f75d6194b597ac32bbdbb7b524a28fb1fa98bd0ddcefce94b313a818cc0

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