AioLogic

0.16.0 · active · verified Thu Apr 16

AioLogic is a GIL-powered locking library for Python (current version 0.16.0) that provides synchronization primitives which are both async-aware and thread-aware. It addresses common challenges in concurrent programming by enabling seamless interaction between asynchronous code (within and across multiple threads/event loops) and synchronous code, and between different synchronous threads, offering a unified API for various concurrency models. The library is actively maintained with a focus on performance and broad compatibility.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how `aiologic.Lock` can synchronize access to a shared resource across multiple asyncio event loops running in different OS threads. Standard `asyncio.Lock` would raise a RuntimeError, and `threading.Lock` would cause a deadlock in such a scenario.

import asyncio
from threading import Thread
from aiologic import Lock

lock = Lock()

async def func(i: int, j: int) -> None:
    print(f"thread={i} task={j} start")
    async with lock:
        await asyncio.sleep(0.1) # Simulate some async work
        print(f"thread={i} task={j} end")

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

# Run two asyncio event loops in separate threads, sharing one aiologic.Lock
thread0 = Thread(target=asyncio.run, args=[main(0)])
thread1 = Thread(target=asyncio.run, args=[main(1)])

thread0.start()
thread1.start()

thread0.join()
thread1.join()
print("All threads and tasks completed.")

view raw JSON →