Skip to content

async_utils

marvin.utilities.async_utils

create_task

Creates async background tasks in a way that is safe from garbage collection.

See https://textual.textualize.io/blog/2023/02/11/the-heisenbug-lurking-in-your-async-code/

Example:

async def my_coro(x: int) -> int: return x + 1

safely submits my_coro for background execution

create_task(my_coro(1))

run_async async

Runs a synchronous function in an asynchronous manner.

run_sync

Runs a coroutine from a synchronous context, either in the current event loop or in a new one if there is no event loop running. The coroutine will block until it is done. A thread will be spawned to run the event loop if necessary, which allows coroutines to run in environments like Jupyter notebooks where the event loop runs on the main thread.