Python functions agnostic towards being called with await or otherwise.
Project description
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:
Project details
Release history Release notifications | RSS feed
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.1.2.tar.gz
(3.0 kB
view details)
Built Distribution
File details
Details for the file asyncgnostic-0.1.2.tar.gz
.
File metadata
- Download URL: asyncgnostic-0.1.2.tar.gz
- Upload date:
- Size: 3.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bedf500e53e83f078347998fcef811015783e4f18dc1e88284c8d109d13f1949 |
|
MD5 | be7276aabbd6050c9398857c37e162f6 |
|
BLAKE2b-256 | 0ca4e08092a8c7613e57cd5ad93c0eb4c16849a49c8352bcf27e047b8b22adcf |
File details
Details for the file asyncgnostic-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: asyncgnostic-0.1.2-py3-none-any.whl
- Upload date:
- Size: 3.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 74c603240643e0f1519874b559980a6a53fc4decb8ece2802576761a034954ff |
|
MD5 | d4b2e67058d6209c016c3e59ca3353cc |
|
BLAKE2b-256 | 572b7ed737e860ed1b297eeb409dd64fa969285004d94a6d2a755175f1fa95cb |