Skip to content

trulens.core.utils.asynchro

trulens.core.utils.asynchro

Synchronization/Async Utilities

NOTE: we cannot name a module "async" as it is a python keyword.

Synchronous vs. Asynchronous

Some functions in TruLens come with asynchronous versions. Those use "async def" instead of "def" and typically start with the letter "a" in their name with the rest matching their synchronous version.

Due to how python handles such functions and how they are executed, it is relatively difficult to reshare code between the two versions. Asynchronous functions are executed by an async loop (see EventLoop). Python prevents any threads from having more than one running loop meaning one may not be able to create one to run some async code if one has already been created/running in the thread. The method sync here, used to convert an async computation into a sync computation, needs to create a new thread. The impact of this, whether overhead, or record info, is uncertain.

What should be Sync/Async?

Try to have all internals be async but for users we may expose sync versions via the sync method. If internals are async and don't need exposure, don't need to provide a synced version.

Attributes

MaybeAwaitable module-attribute

MaybeAwaitable = Union[T, Awaitable[T]]

Awaitable or not.

May be checked with isawaitable.

CallableMaybeAwaitable module-attribute

CallableMaybeAwaitable = Union[
    Callable[[A], B], Callable[[A], Awaitable[B]]
]

Function or coroutine function.

May be checked with is_really_coroutinefunction.

CallableAwaitable module-attribute

CallableAwaitable = Callable[[A], Awaitable[B]]

Function that produces an awaitable / coroutine function.

ThunkMaybeAwaitable module-attribute

ThunkMaybeAwaitable = Union[Thunk[T], Thunk[Awaitable[T]]]

Thunk or coroutine thunk.

May be checked with is_really_coroutinefunction.

Functions

desync async

desync(
    func: CallableMaybeAwaitable[A, T], *args, **kwargs
) -> T

Run the given function asynchronously with the given args. If it is not asynchronous, will run in thread. Note: this has to be marked async since in some cases we cannot tell ahead of time that func is asynchronous so we may end up running it to produce a coroutine object which we then need to run asynchronously.

sync

sync(
    func: CallableMaybeAwaitable[A, T], *args, **kwargs
) -> T

Get result of calling function on the given args. If it is awaitable, will block until it is finished. Runs in a new thread in such cases.