Skip to main content

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:

Metadata

Release files for asyncgnostic 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for asyncgnostic 0.2.0
File Size Uploaded Provenance Actions
asyncgnostic-0.2.0.tar.gz 2.9 kB Without provenance Details for asyncgnostic-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for asyncgnostic 0.2.0
File Interpreter ABI Platform Actions
asyncgnostic-0.2.0-py3-none-any.whl Python 3 none any Details for asyncgnostic-0.2.0-py3-none-any.whl

Total release size: 6.6 kB

Release files / asyncgnostic-0.2.0.tar.gz

Download URL asyncgnostic-0.2.0.tar.gz
Size 2.9 kB
Tags Source
SHA-256 checksum
How to use checksums
d8a92a3bf7628f063fe3d1938902b03da33ca29091cf7a16e570173f2c30910c
BLAKE2b-256 checksum
How to use checksums
252bbd8fa916b45d55f252e32aa998fbba0753ace64a6767ae6e2c126d083f45
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.8

Release files / asyncgnostic-0.2.0-py3-none-any.whl

Download URL asyncgnostic-0.2.0-py3-none-any.whl
Size 3.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ac48db32baa80ed7a1d4d8952784b587642117e6dfb29f8c8194a33e52f048f1
BLAKE2b-256 checksum
How to use checksums
37ffe072c26b6f0b0a6e5605958db04f4f9875f6b97f1aed88ef23da34d22ee9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.8

Security for asyncgnostic 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page