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 concurrent fn_coroutine Tasks allowed to run at the same time. If this limit is exceeded, then the oldest fn_coroutine Task will be cancelled to make room for the new Task. Default is 1. Set to None to allow unlimited concurrency.

Return type:

tuple[Callable[[ParamSpec(_P_async)], None], Callable[[], None]]

Returns:

A tuple pair of non-async functions.
  1. A non-async function with the same argument signature as the fn_coroutine.

  2. A non-async cancellation function which can be called to cancel all of the fn_coroutine Tasks manually.

Will create a new Task with the fn_coroutine coroutine. This Task will be bound to the lifecycle of this @component. The async fn_coroutine function can have any argument signature, but it must return None. The return value is discarded.

The Hook takes an async function fn_coroutine and returns a tuple pair of non-async functions.

  1. 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 calls fn_coroutine.

  2. A non-async cancellation function which can be called to cancel the fn_coroutine Task 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 useAsyncCallback from https://www.npmjs.com/package/react-async-hook

This Hook is similar to create_task() , but it will cancel the Task when this @component is unmounted, or when the concurrency limit is exceeded.