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:
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
asyncgnostic-0.2.0.tar.gz
(2.9 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file asyncgnostic-0.2.0.tar.gz.
File metadata
- Download URL: asyncgnostic-0.2.0.tar.gz
- Upload date:
- Size: 2.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d8a92a3bf7628f063fe3d1938902b03da33ca29091cf7a16e570173f2c30910c
|
|
| MD5 |
f4e1b00ff87def194f84ead4636cfb0d
|
|
| BLAKE2b-256 |
252bbd8fa916b45d55f252e32aa998fbba0753ace64a6767ae6e2c126d083f45
|
File details
Details for the file asyncgnostic-0.2.0-py3-none-any.whl.
File metadata
- Download URL: asyncgnostic-0.2.0-py3-none-any.whl
- Upload date:
- Size: 3.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ac48db32baa80ed7a1d4d8952784b587642117e6dfb29f8c8194a33e52f048f1
|
|
| MD5 |
d17cdcc9855fa0ef8d36621c8cee8889
|
|
| BLAKE2b-256 |
37ffe072c26b6f0b0a6e5605958db04f4f9875f6b97f1aed88ef23da34d22ee9
|