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 | |
Run all asynchronous work for one SDK instance. |
| Class | |
Run functions in a bounded set of daemon threads. |
| Type Variable | T |
Undocumented |
| Type Alias | |
Synchronous exception handler accepted by an SDK event loop. |
| Variable | logger |
Undocumented |
| Class | _ |
Enforce the synchronous SDK handler contract at invocation time. |
| Class | _ |
Identify one borrowed-loop assignment while installation can fail. |
| Class | _ |
Store one function call for a daemon worker. |
| Function | _in |
Return whether the current thread belongs to any SDK executor. |
| Function | _invoke |
Call an SDK handler and report a non-None return value. |
| Function | _publish |
Publish one concurrent result to a weakly retained asyncio waiter. |
| Function | _validate |
Reject a non-callable or directly recognizable async handler. |
| Constant | _BORROWED |
Maximum wait for a supplied loop to install an SDK exception handler. |
| Constant | _BORROWED |
Interval for checking whether a supplied loop stopped during installation. |
| Type Alias | _ |
Exception-handler shape returned by the asyncio loop API. |
| Variable | _current |
Runtime and submission bound to the current SDK task. |
| Variable | _sdk |
Undocumented |
_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:_StoredLoopExceptionHandler | Synchronous handler to call. |
loop:asyncio.AbstractEventLoop | Event loop that reported the exception. |
context:dict[ | Asyncio exception context. |
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[ | Authoritative concurrent completion. |
loopweakref.ReferenceType[ | Weak reference to the waiter's event loop. |
relayweakref.ReferenceType[ | Weak reference to the waiter's relay future. |
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:object | Candidate handler supplied by the caller. |
| Raises | |
TypeError | If the handler is not callable or is declared with async def. |
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.