MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / async_scoped_session

Class async_scoped_session

lib/sqlalchemy/ext/asyncio/scoping.py:122–1679  ·  view source on GitHub ↗

Provides scoped management of :class:`.AsyncSession` objects. See the section :ref:`asyncio_scoped_session` for usage details. .. versionadded:: 1.4.19

Source from the content-addressed store, hash-verified

120 use_intermediate_variable=["get"],
121)
122class async_scoped_session(Generic[_AS]):
123 """Provides scoped management of :class:`.AsyncSession` objects.
124
125 See the section :ref:`asyncio_scoped_session` for usage details.
126
127 .. versionadded:: 1.4.19
128
129
130 """
131
132 _support_async = True
133
134 session_factory: async_sessionmaker[_AS]
135 """The `session_factory` provided to `__init__` is stored in this
136 attribute and may be accessed at a later time. This can be useful when
137 a new non-scoped :class:`.AsyncSession` is needed."""
138
139 registry: ScopedRegistry[_AS]
140
141 def __init__(
142 self,
143 session_factory: async_sessionmaker[_AS],
144 scopefunc: Callable[[], Any],
145 ):
146 """Construct a new :class:`_asyncio.async_scoped_session`.
147
148 :param session_factory: a factory to create new :class:`_asyncio.AsyncSession`
149 instances. This is usually, but not necessarily, an instance
150 of :class:`_asyncio.async_sessionmaker`.
151
152 :param scopefunc: function which defines
153 the current scope. A function such as ``asyncio.current_task``
154 may be useful here.
155
156 """ # noqa: E501
157
158 self.session_factory = session_factory
159 self.registry = ScopedRegistry(session_factory, scopefunc)
160
161 @property
162 def _proxied(self) -> _AS:
163 return self.registry()
164
165 def __call__(self, **kw: Any) -> _AS:
166 r"""Return the current :class:`.AsyncSession`, creating it
167 using the :attr:`.scoped_session.session_factory` if not present.
168
169 :param \**kw: Keyword arguments will be passed to the
170 :attr:`.scoped_session.session_factory` callable, if an existing
171 :class:`.AsyncSession` is not present. If the
172 :class:`.AsyncSession` is present
173 and keyword arguments have been passed,
174 :exc:`~sqlalchemy.exc.InvalidRequestError` is raised.
175
176 """
177 if kw:
178 if self.registry.has():
179 raise sa_exc.InvalidRequestError(

Callers 7

async_stuff.pyFile · 0.90
async_test_exec_optionsFunction · 0.90
test_basicMethod · 0.90
test_attributesMethod · 0.90

Calls

no outgoing calls

Tested by 4

test_basicMethod · 0.72
test_attributesMethod · 0.72