Synchronicity

0.12.1 · active · verified Sun Apr 05

Synchronicity is a Python library designed to simplify the development of libraries that need to offer both synchronous (blocking) and asynchronous (non-blocking) APIs from a single async implementation. It achieves this by creating an event loop on a separate thread, wrapping functions, generators, and classes, allowing them to be called synchronously or asynchronously via a `.aio` attribute. The current version is 0.12.1, and it appears to be actively maintained with regular updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define an asynchronous function and then use `Synchronizer` to expose both a synchronous (blocking) interface and an asynchronous (`.aio`) interface for it. The synchronous call blocks until completion, while the asynchronous call can be awaited within an async context.

import asyncio
from synchronicity import Synchronizer

synchronizer = Synchronizer()

@synchronizer.wrap
async def my_async_function(x):
    await asyncio.sleep(0.1)
    return x**2

# --- Synchronous usage (blocking) ---
print(f"Synchronous call: my_async_function(5) = {my_async_function(5)}") # Blocks until result is ready

# --- Asynchronous usage (non-blocking) ---
async def main_async():
    print(f"Asynchronous call (awaiting): await my_async_function.aio(10) = {await my_async_function.aio(10)}")

    # Example with multiple concurrent calls from an async context
    results = await asyncio.gather(
        my_async_function.aio(2),
        my_async_function.aio(3),
        my_async_function.aio(4)
    )
    print(f"Concurrent async calls: {results}")

asyncio.run(main_async())

view raw JSON →