Asyncer

0.0.17 · active · verified Thu Apr 09

Asyncer is a small Python library built on top of AnyIO, focused on improving developer experience when working with `async` and `await` and mixing async with regular blocking code. It provides utility functions like `asyncify` and `syncify` to bridge asynchronous and synchronous code more conveniently, offering better editor support and type checking. It is actively maintained, with frequent minor releases.

Warnings

Install

Imports

Quickstart

This example demonstrates how to use `asyncer.asyncify` to call a regular blocking function (`blocking_function`) from within an asynchronous context (`main`). `asyncify` ensures the blocking operation runs in a worker thread, preventing it from blocking the main event loop. The `anyio.run(main)` call starts the AnyIO event loop to execute the asynchronous `main` function.

import anyio
from asyncer import asyncify
import time

def blocking_function(name: str) -> str:
    """A blocking function that simulates work."""
    time.sleep(0.1)  # Simulate I/O or CPU-bound work
    return f"Hello from blocking func, {name}!"

async def main():
    print("Starting async main function.")
    # Run a blocking function in a worker thread using asyncify
    message = await asyncify(blocking_function)(name="World")
    print(message)
    print("Finished async main function.")

if __name__ == "__main__":
    anyio.run(main)

view raw JSON →