summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/util/queue.py
diff options
context:
space:
mode:
authorTaem Park <wwwee98@gmail.com>2018-08-21 10:54:45 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2018-09-18 21:58:02 -0400
commitb64a3dd87a4204ce6d4f2793a7a3f7fbf72eb01f (patch)
tree8de8e0e60e63228beae54378eae0e990b4a9ce3c /lib/sqlalchemy/util/queue.py
parent67a2cd92295bef55d914a5c560b4cead5d456837 (diff)
downloadsqlalchemy-b64a3dd87a4204ce6d4f2793a7a3f7fbf72eb01f.tar.gz
Add LIFO for connection pooling
Added new "lifo" mode to :class:`.QueuePool`, typically enabled by setting the flag :paramref:`.create_engine.pool_use_lifo` to True. "lifo" mode means the same connection just checked in will be the first to be checked out again, allowing excess connections to be cleaned up from the server side during periods of the pool being only partially utilized. Pull request courtesy Taem Park. Change-Id: Idb5e299c5082b3e6b547bd03022acf65fdc34f35 Pull-request: https://github.com/zzzeek/sqlalchemy/pull/467
Diffstat (limited to 'lib/sqlalchemy/util/queue.py')
-rw-r--r--lib/sqlalchemy/util/queue.py13
1 files changed, 11 insertions, 2 deletions
diff --git a/lib/sqlalchemy/util/queue.py b/lib/sqlalchemy/util/queue.py
index 1958702c7..640f70ea9 100644
--- a/lib/sqlalchemy/util/queue.py
+++ b/lib/sqlalchemy/util/queue.py
@@ -39,10 +39,12 @@ class Full(Exception):
class Queue:
- def __init__(self, maxsize=0):
+ def __init__(self, maxsize=0, use_lifo=False):
"""Initialize a queue object with a given maximum size.
If `maxsize` is <= 0, the queue size is infinite.
+
+ If `use_lifo` is True, this Queue acts like a Stack (LIFO).
"""
self._init(maxsize)
@@ -57,6 +59,8 @@ class Queue:
# Notify not_full whenever an item is removed from the queue;
# a thread waiting to put is notified then.
self.not_full = threading.Condition(self.mutex)
+ # If this queue uses LIFO or FIFO
+ self.use_lifo = use_lifo
def qsize(self):
"""Return the approximate size of the queue (not reliable!)."""
@@ -196,4 +200,9 @@ class Queue:
# Get an item from the queue
def _get(self):
- return self.queue.popleft()
+ if self.use_lifo:
+ # LIFO
+ return self.queue.pop()
+ else:
+ # FIFO
+ return self.queue.popleft()