uf.async_support#
Async function support for uf.
Provides utilities for detecting and handling async functions, allowing seamless integration of async def functions in uf apps.
Functions
|
Convert an async function to a synchronous wrapper. |
|
Execute multiple async function calls concurrently. |
|
Create a handler for async functions in web frameworks. |
Detect if the web framework supports async natively. |
|
|
Check if a function is async. |
Check if the framework supports async handlers natively. |
|
|
Convert any async functions in the list to sync wrappers. |
|
Decorator to add retry logic to async functions. |
|
Decorator to add timeout to async functions. |
Classes
Context manager for handling async operations in uf. |
|
|
Wrapper for async functions that provides both sync and async access. |
- class uf.async_support.AsyncContext[source]#
Bases:
objectContext manager for handling async operations in uf.
This helps manage event loops and async resources properly.
Example
>>> async def my_async_function(param): ... return param * 2 >>> with AsyncContext() as ctx: ... ctx.run(my_async_function(21)) 42
- class uf.async_support.AsyncFunctionWrapper(func)[source]#
Bases:
objectWrapper for async functions that provides both sync and async access.
This allows the same function to be called either way, depending on the context.
Example
>>> async def fetch_user(user_id: int): ... return {'id': user_id} >>> >>> wrapper = AsyncFunctionWrapper(fetch_user) >>> wrapper.call_sync(user_id=123) # sync call {'id': 123} >>> import asyncio >>> asyncio.run(wrapper.call_async(user_id=123)) # async call (use await in async code) {'id': 123}
- uf.async_support.async_to_sync(async_func)[source]#
Convert an async function to a synchronous wrapper.
- Parameters:
async_func (
Callable) – Async function to wrap- Return type:
- Returns:
Synchronous wrapper function
Example
>>> async def fetch_data(url: str): ... return {'url': url} >>> sync_fetch = async_to_sync(fetch_data) >>> sync_fetch('https://example.com') {'url': 'https://example.com'}
- uf.async_support.batch_async_calls(async_func, calls)[source]#
Execute multiple async function calls concurrently.
- Parameters:
- Return type:
- Returns:
List of results in same order as calls
Example
>>> async def fetch_user(user_id: int): ... return {'id': user_id} >>> >>> calls = [ ... {'args': (), 'kwargs': {'user_id': 1}}, ... {'args': (), 'kwargs': {'user_id': 2}}, ... {'args': (), 'kwargs': {'user_id': 3}}, ... ] >>> batch_async_calls(fetch_user, calls) [{'id': 1}, {'id': 2}, {'id': 3}]
- uf.async_support.create_async_handler(async_func)[source]#
Create a handler for async functions in web frameworks.
This is useful for integrating with frameworks that may or may not support async natively (like Bottle vs FastAPI).
- Parameters:
async_func (
Callable) – Async function to create handler for- Return type:
- Returns:
Appropriate handler (async or sync wrapped)
Example
>>> async def my_endpoint(param: str): ... result = await some_async_operation(param) ... return result >>> handler = create_async_handler(my_endpoint)
- uf.async_support.detect_async_framework(app)[source]#
Detect if the web framework supports async natively.
- Parameters:
app – The web application
- Returns:
‘fastapi’, ‘bottle’, ‘unknown’
- Return type:
- uf.async_support.is_async_function(func)[source]#
Check if a function is async.
- Parameters:
func (
Callable) – Function to check- Return type:
- Returns:
True if function is async
Example
>>> async def my_async_func(): ... pass >>> is_async_function(my_async_func) True
- uf.async_support.is_framework_async_capable(app)[source]#
Check if the framework supports async handlers natively.
- Parameters:
app – The web application
- Return type:
- Returns:
True if framework supports async
- uf.async_support.make_sync_compatible(funcs)[source]#
Convert any async functions in the list to sync wrappers.
- Parameters:
funcs (
list[Callable]) – List of functions (may include async)- Return type:
- Returns:
List of functions (all synchronous)
Example
>>> def sync_func(): return 1 >>> async def async_func(): return 2 >>> compatible = make_sync_compatible([sync_func, async_func]) >>> [f() for f in compatible] # all callable synchronously [1, 2]
- uf.async_support.retry_async(max_attempts=3, delay=1.0)[source]#
Decorator to add retry logic to async functions.
- Parameters:
- Returns:
Decorator function
Example
>>> @retry_async(max_attempts=3, delay=2.0) ... async def unreliable_api_call(): ... # May fail, will retry ... return await external_api.fetch()