edifice.use_async_call#
- edifice.use_async_call(fn_coroutine, max_concurrent=1)[source]#
Hook to call an async function from a non-async context.
- Parameters:
fn_coroutine (
Callable[[ParamSpec(_P_async)],Coroutine[None,None,None]]) – Async Coroutine function to be run as a Task.max_concurrent (
int|None) – Maximum number of concurrentfn_coroutineTasks allowed to run at the same time. If this limit is exceeded, then the oldestfn_coroutineTask will be cancelled to make room for the new Task. Default is1. Set toNoneto allow unlimited concurrency.
- Return type:
tuple[Callable[[ParamSpec(_P_async)],None],Callable[[],None]]- Returns:
- A tuple pair of non-async functions.
A non-async function with the same argument signature as the
fn_coroutine.A non-async cancellation function which can be called to cancel all of the
fn_coroutineTasks manually.
Will create a new Task with the
fn_coroutinecoroutine. This Task will be bound to the lifecycle of this@component. The asyncfn_coroutinefunction can have any argument signature, but it must returnNone. The return value is discarded.The Hook takes an async function
fn_coroutineand returns a tuple pair of non-async functions.A non-async function with the same argument signature as the
fn_coroutine. When called, this non-async function will start a new Task an the main Edifice thread event loop which callsfn_coroutine.A non-async cancellation function which can be called to cancel the
fn_coroutineTask manually.
use_async_call to delay print#async def delay_print_async(message:str): await asyncio.sleep(1) print(message) delay_print, cancel_print = use_async_call(delay_print_async) Button( text="Print Hello World after 1 second", on_click=lambda _: delay_print("Hello World") ) Button( text="Cancel print", on_click=lambda _: cancel_print() )
This Hook is similar to
useAsyncCallbackfrom https://www.npmjs.com/package/react-async-hookThis Hook is similar to create_task() , but it will cancel the Task when this
@componentis unmounted, or when the concurrency limit is exceeded.