module documentation

Run SDK asynchronous work on one event loop.

The runtime can own an event loop or use a loop that the caller supplies. An owned runtime also owns a daemon thread pool. The runtime converts submitted work to awaitables that callers can use from other event loops.

Runtime state moves in one direction: accepting submissions, preparing close, then shut down. _shutdown_lock protects submission acceptance and the terminal shutdown flag. When code needs both it and _submissions_lock, it always acquires them in that order. The background and shutdown-preparation locks protect independent sets and are never held while another thread is joined or a future result is awaited.

_active_tasks, _protected_tasks, and _task_submissions belong only to the SDK event-loop thread. They need no thread lock. Code must not acquire a runtime lock and then synchronously wait for work that needs the SDK loop. This rule keeps shutdown callbacks able to make progress.

Class AsyncRuntime Run all asynchronous work for one SDK instance.
Class DaemonThreadPoolExecutor Run functions in a bounded set of daemon threads.
Type Variable T Undocumented
Type Alias LoopExceptionHandler Synchronous exception handler accepted by an SDK event loop.
Variable logger Undocumented
Class _CheckedLoopExceptionHandler Enforce the synchronous SDK handler contract at invocation time.
Class _StagedLoopExceptionHandler Identify one borrowed-loop assignment while installation can fail.
Class _WorkItem Store one function call for a daemon worker.
Function _in_sdk_executor_thread Return whether the current thread belongs to any SDK executor.
Function _invoke_loop_exception_handler Call an SDK handler and report a non-None return value.
Function _publish_cross_loop_waiter Publish one concurrent result to a weakly retained asyncio waiter.
Function _validate_loop_exception_handler Reject a non-callable or directly recognizable async handler.
Constant _BORROWED_LOOP_HANDLER_INSTALL_TIMEOUT_SECONDS Maximum wait for a supplied loop to install an SDK exception handler.
Constant _BORROWED_LOOP_HANDLER_POLL_SECONDS Interval for checking whether a supplied loop stopped during installation.
Type Alias _StoredLoopExceptionHandler Exception-handler shape returned by the asyncio loop API.
Variable _current_submission Runtime and submission bound to the current SDK task.
Variable _sdk_executor_worker Undocumented

Undocumented

Value
TypeVar('T')
LoopExceptionHandler = (source)

Synchronous exception handler accepted by an SDK event loop.

Value
Callable[[asyncio.AbstractEventLoop, dict[str, Any]], None]

Undocumented

def _in_sdk_executor_thread() -> bool: (source)

Return whether the current thread belongs to any SDK executor.

def _invoke_loop_exception_handler(handler: _StoredLoopExceptionHandler, loop: asyncio.AbstractEventLoop, context: dict[str, Any]): (source)

Call an SDK handler and report a non-None return value.

Static validation cannot identify a synchronous wrapper that returns a coroutine. This invocation boundary closes a native coroutine only when it has not started, before it can produce an unawaited-coroutine warning. It does not change a suspended coroutine, Future, Task, or opaque awaitable because the handler might not own that work. It sends the original context and the contract violation to asyncio's default handler because the SDK cannot know whether an invalid handler processed the original diagnostic. The default handler is used directly to avoid recursively invoking the invalid custom handler.

Parameters
handler:_StoredLoopExceptionHandlerSynchronous handler to call.
loop:asyncio.AbstractEventLoopEvent loop that reported the exception.
context:dict[str, Any]Asyncio exception context.
def _publish_cross_loop_waiter(source: Future[T], loop_ref: weakref.ReferenceType[asyncio.AbstractEventLoop], relay_ref: weakref.ReferenceType[asyncio.Future[T]]): (source)

Publish one concurrent result to a weakly retained asyncio waiter.

Parameters
source:Future[T]Authoritative concurrent completion.
loop_ref:weakref.ReferenceType[asyncio.AbstractEventLoop]Weak reference to the waiter's event loop.
relay_ref:weakref.ReferenceType[asyncio.Future[T]]Weak reference to the waiter's relay future.
def _validate_loop_exception_handler(handler: object): (source)

Reject a non-callable or directly recognizable async handler.

Static inspection cannot identify a synchronous wrapper that returns an asynchronous value. The invocation wrapper closes a newly returned, unstarted native coroutine, leaves other awaitables under their existing ownership, and sends the original context and every non-None return to asyncio's default exception handler.

Parameters
handler:objectCandidate handler supplied by the caller.
Raises
TypeErrorIf the handler is not callable or is declared with async def.
_BORROWED_LOOP_HANDLER_INSTALL_TIMEOUT_SECONDS: float = (source)

Maximum wait for a supplied loop to install an SDK exception handler.

Value
30.0
_BORROWED_LOOP_HANDLER_POLL_SECONDS: float = (source)

Interval for checking whether a supplied loop stopped during installation.

Value
0.1
_StoredLoopExceptionHandler = (source)

Exception-handler shape returned by the asyncio loop API.

Value
Callable[[asyncio.AbstractEventLoop, dict[str, Any]], object]

Runtime and submission bound to the current SDK task.

The key is module-level, as required by contextvars. Its value belongs to an execution context rather than to the process as a whole. Storing the runtime beside the submission lets several SDKs safely share a thread or event loop: a runtime accepts only a binding whose runtime identity matches itself.

_sdk_executor_worker = (source)

Undocumented