This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
# pool/impl.py
|
||||
# Copyright (C) 2005-2025 the SQLAlchemy authors and contributors
|
||||
# sqlalchemy/pool.py
|
||||
# Copyright (C) 2005-2023 the SQLAlchemy authors and contributors
|
||||
# <see AUTHORS file>
|
||||
#
|
||||
# This module is part of SQLAlchemy and is released under
|
||||
# the MIT License: https://www.opensource.org/licenses/mit-license.php
|
||||
|
||||
|
||||
"""Pool implementation classes."""
|
||||
"""Pool implementation classes.
|
||||
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import threading
|
||||
@@ -41,30 +43,21 @@ if typing.TYPE_CHECKING:
|
||||
|
||||
|
||||
class QueuePool(Pool):
|
||||
|
||||
"""A :class:`_pool.Pool`
|
||||
that imposes a limit on the number of open connections.
|
||||
|
||||
:class:`.QueuePool` is the default pooling implementation used for
|
||||
all :class:`_engine.Engine` objects other than SQLite with a ``:memory:``
|
||||
database.
|
||||
|
||||
The :class:`.QueuePool` class **is not compatible** with asyncio and
|
||||
:func:`_asyncio.create_async_engine`. The
|
||||
:class:`.AsyncAdaptedQueuePool` class is used automatically when
|
||||
using :func:`_asyncio.create_async_engine`, if no other kind of pool
|
||||
is specified.
|
||||
|
||||
.. seealso::
|
||||
|
||||
:class:`.AsyncAdaptedQueuePool`
|
||||
all :class:`_engine.Engine` objects, unless the SQLite dialect is
|
||||
in use with a ``:memory:`` database.
|
||||
|
||||
"""
|
||||
|
||||
_is_asyncio = False
|
||||
_is_asyncio = False # type: ignore[assignment]
|
||||
|
||||
_queue_class: Type[sqla_queue.QueueCommon[ConnectionPoolEntry]] = (
|
||||
sqla_queue.Queue
|
||||
)
|
||||
_queue_class: Type[
|
||||
sqla_queue.QueueCommon[ConnectionPoolEntry]
|
||||
] = sqla_queue.Queue
|
||||
|
||||
_pool: sqla_queue.QueueCommon[ConnectionPoolEntry]
|
||||
|
||||
@@ -131,7 +124,6 @@ class QueuePool(Pool):
|
||||
:class:`_pool.Pool` constructor.
|
||||
|
||||
"""
|
||||
|
||||
Pool.__init__(self, creator, **kw)
|
||||
self._pool = self._queue_class(pool_size, use_lifo=use_lifo)
|
||||
self._overflow = 0 - pool_size
|
||||
@@ -257,31 +249,20 @@ class QueuePool(Pool):
|
||||
|
||||
|
||||
class AsyncAdaptedQueuePool(QueuePool):
|
||||
"""An asyncio-compatible version of :class:`.QueuePool`.
|
||||
|
||||
This pool is used by default when using :class:`.AsyncEngine` engines that
|
||||
were generated from :func:`_asyncio.create_async_engine`. It uses an
|
||||
asyncio-compatible queue implementation that does not use
|
||||
``threading.Lock``.
|
||||
|
||||
The arguments and operation of :class:`.AsyncAdaptedQueuePool` are
|
||||
otherwise identical to that of :class:`.QueuePool`.
|
||||
|
||||
"""
|
||||
|
||||
_is_asyncio = True
|
||||
_queue_class: Type[sqla_queue.QueueCommon[ConnectionPoolEntry]] = (
|
||||
sqla_queue.AsyncAdaptedQueue
|
||||
)
|
||||
_is_asyncio = True # type: ignore[assignment]
|
||||
_queue_class: Type[
|
||||
sqla_queue.QueueCommon[ConnectionPoolEntry]
|
||||
] = sqla_queue.AsyncAdaptedQueue
|
||||
|
||||
_dialect = _AsyncConnDialect()
|
||||
|
||||
|
||||
class FallbackAsyncAdaptedQueuePool(AsyncAdaptedQueuePool):
|
||||
_queue_class = sqla_queue.FallbackAsyncAdaptedQueue # type: ignore[assignment] # noqa: E501
|
||||
_queue_class = sqla_queue.FallbackAsyncAdaptedQueue
|
||||
|
||||
|
||||
class NullPool(Pool):
|
||||
|
||||
"""A Pool which does not pool connections.
|
||||
|
||||
Instead it literally opens and closes the underlying DB-API connection
|
||||
@@ -291,9 +272,6 @@ class NullPool(Pool):
|
||||
invalidation are not supported by this Pool implementation, since
|
||||
no connections are held persistently.
|
||||
|
||||
The :class:`.NullPool` class **is compatible** with asyncio and
|
||||
:func:`_asyncio.create_async_engine`.
|
||||
|
||||
"""
|
||||
|
||||
def status(self) -> str:
|
||||
@@ -324,6 +302,7 @@ class NullPool(Pool):
|
||||
|
||||
|
||||
class SingletonThreadPool(Pool):
|
||||
|
||||
"""A Pool that maintains one connection per thread.
|
||||
|
||||
Maintains one connection per each thread, never moving a connection to a
|
||||
@@ -341,9 +320,6 @@ class SingletonThreadPool(Pool):
|
||||
scenarios using a SQLite ``:memory:`` database and is not recommended
|
||||
for production use.
|
||||
|
||||
The :class:`.SingletonThreadPool` class **is not compatible** with asyncio
|
||||
and :func:`_asyncio.create_async_engine`.
|
||||
|
||||
|
||||
Options are the same as those of :class:`_pool.Pool`, as well as:
|
||||
|
||||
@@ -356,7 +332,7 @@ class SingletonThreadPool(Pool):
|
||||
|
||||
"""
|
||||
|
||||
_is_asyncio = False
|
||||
_is_asyncio = False # type: ignore[assignment]
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@@ -446,14 +422,13 @@ class SingletonThreadPool(Pool):
|
||||
|
||||
|
||||
class StaticPool(Pool):
|
||||
|
||||
"""A Pool of exactly one connection, used for all requests.
|
||||
|
||||
Reconnect-related functions such as ``recycle`` and connection
|
||||
invalidation (which is also used to support auto-reconnect) are only
|
||||
partially supported right now and may not yield good results.
|
||||
|
||||
The :class:`.StaticPool` class **is compatible** with asyncio and
|
||||
:func:`_asyncio.create_async_engine`.
|
||||
|
||||
"""
|
||||
|
||||
@@ -511,6 +486,7 @@ class StaticPool(Pool):
|
||||
|
||||
|
||||
class AssertionPool(Pool):
|
||||
|
||||
"""A :class:`_pool.Pool` that allows at most one checked out connection at
|
||||
any given time.
|
||||
|
||||
@@ -518,9 +494,6 @@ class AssertionPool(Pool):
|
||||
at a time. Useful for debugging code that is using more connections
|
||||
than desired.
|
||||
|
||||
The :class:`.AssertionPool` class **is compatible** with asyncio and
|
||||
:func:`_asyncio.create_async_engine`.
|
||||
|
||||
"""
|
||||
|
||||
_conn: Optional[ConnectionPoolEntry]
|
||||
|
||||
Reference in New Issue
Block a user