This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
# ext/asyncio/scoping.py
|
||||
# Copyright (C) 2005-2025 the SQLAlchemy authors and contributors
|
||||
# Copyright (C) 2005-2023 the SQLAlchemy authors and contributors
|
||||
# <see AUTHORS file>
|
||||
#
|
||||
# This module is part of SQLAlchemy and is released under
|
||||
@@ -364,7 +364,7 @@ class async_scoped_session(Generic[_AS]):
|
||||
object is entered::
|
||||
|
||||
async with async_session.begin():
|
||||
... # ORM transaction is begun
|
||||
# .. ORM transaction is begun
|
||||
|
||||
Note that database IO will not normally occur when the session-level
|
||||
transaction is begun, as database transactions begin on an
|
||||
@@ -536,7 +536,8 @@ class async_scoped_session(Generic[_AS]):
|
||||
bind_arguments: Optional[_BindArguments] = None,
|
||||
_parent_execute_state: Optional[Any] = None,
|
||||
_add_event: Optional[Any] = None,
|
||||
) -> Result[_T]: ...
|
||||
) -> Result[_T]:
|
||||
...
|
||||
|
||||
@overload
|
||||
async def execute(
|
||||
@@ -548,7 +549,8 @@ class async_scoped_session(Generic[_AS]):
|
||||
bind_arguments: Optional[_BindArguments] = None,
|
||||
_parent_execute_state: Optional[Any] = None,
|
||||
_add_event: Optional[Any] = None,
|
||||
) -> CursorResult[Any]: ...
|
||||
) -> CursorResult[Any]:
|
||||
...
|
||||
|
||||
@overload
|
||||
async def execute(
|
||||
@@ -560,7 +562,8 @@ class async_scoped_session(Generic[_AS]):
|
||||
bind_arguments: Optional[_BindArguments] = None,
|
||||
_parent_execute_state: Optional[Any] = None,
|
||||
_add_event: Optional[Any] = None,
|
||||
) -> Result[Any]: ...
|
||||
) -> Result[Any]:
|
||||
...
|
||||
|
||||
async def execute(
|
||||
self,
|
||||
@@ -808,28 +811,28 @@ class async_scoped_session(Generic[_AS]):
|
||||
|
||||
# construct async engines w/ async drivers
|
||||
engines = {
|
||||
"leader": create_async_engine("sqlite+aiosqlite:///leader.db"),
|
||||
"other": create_async_engine("sqlite+aiosqlite:///other.db"),
|
||||
"follower1": create_async_engine("sqlite+aiosqlite:///follower1.db"),
|
||||
"follower2": create_async_engine("sqlite+aiosqlite:///follower2.db"),
|
||||
'leader':create_async_engine("sqlite+aiosqlite:///leader.db"),
|
||||
'other':create_async_engine("sqlite+aiosqlite:///other.db"),
|
||||
'follower1':create_async_engine("sqlite+aiosqlite:///follower1.db"),
|
||||
'follower2':create_async_engine("sqlite+aiosqlite:///follower2.db"),
|
||||
}
|
||||
|
||||
|
||||
class RoutingSession(Session):
|
||||
def get_bind(self, mapper=None, clause=None, **kw):
|
||||
# within get_bind(), return sync engines
|
||||
if mapper and issubclass(mapper.class_, MyOtherClass):
|
||||
return engines["other"].sync_engine
|
||||
return engines['other'].sync_engine
|
||||
elif self._flushing or isinstance(clause, (Update, Delete)):
|
||||
return engines["leader"].sync_engine
|
||||
return engines['leader'].sync_engine
|
||||
else:
|
||||
return engines[
|
||||
random.choice(["follower1", "follower2"])
|
||||
random.choice(['follower1','follower2'])
|
||||
].sync_engine
|
||||
|
||||
|
||||
# apply to AsyncSession using sync_session_class
|
||||
AsyncSessionMaker = async_sessionmaker(sync_session_class=RoutingSession)
|
||||
AsyncSessionMaker = async_sessionmaker(
|
||||
sync_session_class=RoutingSession
|
||||
)
|
||||
|
||||
The :meth:`_orm.Session.get_bind` method is called in a non-asyncio,
|
||||
implicitly non-blocking context in the same manner as ORM event hooks
|
||||
@@ -864,7 +867,7 @@ class async_scoped_session(Generic[_AS]):
|
||||
|
||||
This method retrieves the history for each instrumented
|
||||
attribute on the instance and performs a comparison of the current
|
||||
value to its previously flushed or committed value, if any.
|
||||
value to its previously committed value, if any.
|
||||
|
||||
It is in effect a more expensive and accurate
|
||||
version of checking for the given instance in the
|
||||
@@ -1012,7 +1015,8 @@ class async_scoped_session(Generic[_AS]):
|
||||
execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
|
||||
bind_arguments: Optional[_BindArguments] = None,
|
||||
**kw: Any,
|
||||
) -> Optional[_T]: ...
|
||||
) -> Optional[_T]:
|
||||
...
|
||||
|
||||
@overload
|
||||
async def scalar(
|
||||
@@ -1023,7 +1027,8 @@ class async_scoped_session(Generic[_AS]):
|
||||
execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
|
||||
bind_arguments: Optional[_BindArguments] = None,
|
||||
**kw: Any,
|
||||
) -> Any: ...
|
||||
) -> Any:
|
||||
...
|
||||
|
||||
async def scalar(
|
||||
self,
|
||||
@@ -1065,7 +1070,8 @@ class async_scoped_session(Generic[_AS]):
|
||||
execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
|
||||
bind_arguments: Optional[_BindArguments] = None,
|
||||
**kw: Any,
|
||||
) -> ScalarResult[_T]: ...
|
||||
) -> ScalarResult[_T]:
|
||||
...
|
||||
|
||||
@overload
|
||||
async def scalars(
|
||||
@@ -1076,7 +1082,8 @@ class async_scoped_session(Generic[_AS]):
|
||||
execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
|
||||
bind_arguments: Optional[_BindArguments] = None,
|
||||
**kw: Any,
|
||||
) -> ScalarResult[Any]: ...
|
||||
) -> ScalarResult[Any]:
|
||||
...
|
||||
|
||||
async def scalars(
|
||||
self,
|
||||
@@ -1175,7 +1182,8 @@ class async_scoped_session(Generic[_AS]):
|
||||
Proxied for the :class:`_asyncio.AsyncSession` class on
|
||||
behalf of the :class:`_asyncio.scoping.async_scoped_session` class.
|
||||
|
||||
Raises :class:`_exc.NoResultFound` if the query selects no rows.
|
||||
Raises ``sqlalchemy.orm.exc.NoResultFound`` if the query selects
|
||||
no rows.
|
||||
|
||||
..versionadded: 2.0.22
|
||||
|
||||
@@ -1205,7 +1213,8 @@ class async_scoped_session(Generic[_AS]):
|
||||
execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
|
||||
bind_arguments: Optional[_BindArguments] = None,
|
||||
**kw: Any,
|
||||
) -> AsyncResult[_T]: ...
|
||||
) -> AsyncResult[_T]:
|
||||
...
|
||||
|
||||
@overload
|
||||
async def stream(
|
||||
@@ -1216,7 +1225,8 @@ class async_scoped_session(Generic[_AS]):
|
||||
execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
|
||||
bind_arguments: Optional[_BindArguments] = None,
|
||||
**kw: Any,
|
||||
) -> AsyncResult[Any]: ...
|
||||
) -> AsyncResult[Any]:
|
||||
...
|
||||
|
||||
async def stream(
|
||||
self,
|
||||
@@ -1255,7 +1265,8 @@ class async_scoped_session(Generic[_AS]):
|
||||
execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
|
||||
bind_arguments: Optional[_BindArguments] = None,
|
||||
**kw: Any,
|
||||
) -> AsyncScalarResult[_T]: ...
|
||||
) -> AsyncScalarResult[_T]:
|
||||
...
|
||||
|
||||
@overload
|
||||
async def stream_scalars(
|
||||
@@ -1266,7 +1277,8 @@ class async_scoped_session(Generic[_AS]):
|
||||
execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
|
||||
bind_arguments: Optional[_BindArguments] = None,
|
||||
**kw: Any,
|
||||
) -> AsyncScalarResult[Any]: ...
|
||||
) -> AsyncScalarResult[Any]:
|
||||
...
|
||||
|
||||
async def stream_scalars(
|
||||
self,
|
||||
|
||||
Reference in New Issue
Block a user