scoping.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # ext/asyncio/scoping.py
  2. # Copyright (C) 2005-2022 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. from .session import AsyncSession
  8. from ...orm.scoping import ScopedSessionMixin
  9. from ...util import create_proxy_methods
  10. from ...util import ScopedRegistry
  11. @create_proxy_methods(
  12. AsyncSession,
  13. ":class:`_asyncio.AsyncSession`",
  14. ":class:`_asyncio.scoping.async_scoped_session`",
  15. classmethods=["close_all", "object_session", "identity_key"],
  16. methods=[
  17. "__contains__",
  18. "__iter__",
  19. "add",
  20. "add_all",
  21. "begin",
  22. "begin_nested",
  23. "close",
  24. "commit",
  25. "connection",
  26. "delete",
  27. "execute",
  28. "expire",
  29. "expire_all",
  30. "expunge",
  31. "expunge_all",
  32. "flush",
  33. "get",
  34. "get_bind",
  35. "is_modified",
  36. "invalidate",
  37. "merge",
  38. "refresh",
  39. "rollback",
  40. "scalar",
  41. "scalars",
  42. "stream",
  43. "stream_scalars",
  44. ],
  45. attributes=[
  46. "bind",
  47. "dirty",
  48. "deleted",
  49. "new",
  50. "identity_map",
  51. "is_active",
  52. "autoflush",
  53. "no_autoflush",
  54. "info",
  55. ],
  56. )
  57. class async_scoped_session(ScopedSessionMixin):
  58. """Provides scoped management of :class:`.AsyncSession` objects.
  59. See the section :ref:`asyncio_scoped_session` for usage details.
  60. .. versionadded:: 1.4.19
  61. """
  62. _support_async = True
  63. def __init__(self, session_factory, scopefunc):
  64. """Construct a new :class:`_asyncio.async_scoped_session`.
  65. :param session_factory: a factory to create new :class:`_asyncio.AsyncSession`
  66. instances. This is usually, but not necessarily, an instance
  67. of :class:`_orm.sessionmaker` which itself was passed the
  68. :class:`_asyncio.AsyncSession` to its :paramref:`_orm.sessionmaker.class_`
  69. parameter::
  70. async_session_factory = sessionmaker(some_async_engine, class_= AsyncSession)
  71. AsyncSession = async_scoped_session(async_session_factory, scopefunc=current_task)
  72. :param scopefunc: function which defines
  73. the current scope. A function such as ``asyncio.current_task``
  74. may be useful here.
  75. """ # noqa E501
  76. self.session_factory = session_factory
  77. self.registry = ScopedRegistry(session_factory, scopefunc)
  78. @property
  79. def _proxied(self):
  80. return self.registry()
  81. async def remove(self):
  82. """Dispose of the current :class:`.AsyncSession`, if present.
  83. Different from scoped_session's remove method, this method would use
  84. await to wait for the close method of AsyncSession.
  85. """
  86. if self.registry.has():
  87. await self.registry().close()
  88. self.registry.clear()