MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / await_

Function await_

lib/sqlalchemy/util/concurrency.py:147–171  ·  view source on GitHub ↗

Awaits an async function in a sync method. The sync method must be inside a :func:`greenlet_spawn` context. :func:`await_` calls cannot be nested. :param awaitable: The coroutine to call.

(awaitable: Awaitable[_T])

Source from the content-addressed store, hash-verified

145
146
147def await_(awaitable: Awaitable[_T]) -> _T:
148 """Awaits an async function in a sync method.
149
150 The sync method must be inside a :func:`greenlet_spawn` context.
151 :func:`await_` calls cannot be nested.
152
153 :param awaitable: The coroutine to call.
154
155 """
156 # this is called in the context greenlet while running fn
157 current = _concurrency_shim.getcurrent()
158 if not getattr(current, "__sqlalchemy_greenlet_provider__", False):
159 _safe_cancel_awaitable(awaitable)
160
161 raise exc.MissingGreenlet(
162 "greenlet_spawn has not been called; can't call await_() "
163 "here. Was IO attempted in an unexpected place?"
164 )
165
166 # returns the control to the driver greenlet passing it
167 # a coroutine to run. Once the awaitable is done, the driver greenlet
168 # switches back to this greenlet with the result of awaitable that is
169 # then returned to the caller (or raised as error)
170 assert current.parent
171 return current.parent.switch(awaitable) # type: ignore[no-any-return]
172
173
174await_only = await_ # old name. deprecated on 2.2

Callers 15

goFunction · 0.90
sync_methMethod · 0.90
goMethod · 0.90
inner_awaitMethod · 0.90
innerMethod · 0.90
_read_lobMethod · 0.90
set_autocommitMethod · 0.85
set_isolation_levelMethod · 0.85
set_read_onlyMethod · 0.85

Calls 1

_safe_cancel_awaitableFunction · 0.85

Tested by 9

goFunction · 0.72
sync_methMethod · 0.72
goMethod · 0.72
inner_awaitMethod · 0.72
innerMethod · 0.72
_read_lobMethod · 0.72