/asyncgnostic

Python functions agnostic towards being called with await or otherwise.

Primary LanguagePythonMIT LicenseMIT

asyncgnostic

Python functions agnostic towards being called with await or otherwise.

Uses multiple dispatch to automatically call asynchronous or synchronous function based on calling context.

Example:

Automatic sync/async dispatch

import asyncio
from asyncgnostic import awaitable


# Define a sync function
def handler() -> str:  # type: ignore
    return "Running Sync"


# Pair the sync function with an async function
@awaitable(handler)
async def handler() -> str:
    return "Running Async"


# Call the function from sync context
def sync_main():
    print("sync context:", handler())


# Call the function from async context
async def async_main():
    print("async context:", await handler())


# Run the sync and async functions
sync_main()
asyncio.run(async_main())

Output:

sync context: Running Sync
async context: Running Async

Detect context in your function for sync/async dispatch dispatch

import asyncio
from asyncgnostic import awaited


# Define a sync function
def sync_handler() -> str:  # type: ignore
    return "Running Sync"


# Define an async function
async def async_handler() -> str:
    return "Running Async"


# Define a dispatcher
def handler() -> str:
    if awaited():
        # If called from async context, call async function
        return async_handler()
    else:
        # If called from sync context, call sync function
        return sync_handler()


# Call the function from sync context
def sync_main():
    print("sync context", handler())


# Call the function from async context
async def async_main():
    print("async context:", await handler())


# Run the sync and async functions
sync_main()
asyncio.run(async_main())

Output:

sync context: Running Sync
async context: Running Async

Install

pip install asyncgnostic

Credits:

Gratefully borrowed improvements from curio.

Reference: