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)
| File | Size | Uploaded | |
|---|---|---|---|
| asyncgnostic-0.2.0.tar.gz | 2.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| asyncgnostic-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
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
|