uqlm.utils.async_utils.run_sync#

uqlm.utils.async_utils.run_sync(coro)#

Run a coroutine to completion from synchronous code and return its result.

This is used to offer blocking, non-async counterparts (e.g. generate_and_score_sync) to methods that are otherwise only available as async def coroutines, so that users who are not working within an event loop are not required to write async/await boilerplate themselves.

If the calling thread has no running event loop, the coroutine is executed directly with asyncio.run. If the calling thread already has a running event loop–for example, Jupyter/IPython kernels run a persistent event loop in the main thread–asyncio.run cannot be used directly since it raises RuntimeError: asyncio.run() cannot be called from a running event loop. In that case, the coroutine is instead executed to completion on a fresh event loop in a dedicated worker thread so it does not conflict with the caller’s running loop.

Return type:

TypeVar(T)

Parameters:

coro (Coroutine) – The coroutine to execute (e.g. the object returned by calling an async def method, prior to awaiting it).

Returns:

The value returned upon completion of coro.

Return type:

Any