Change ClientPool to properly block when a client isn't available
This commit is contained in:
parent
3331c59d77
commit
0e383d7289
15
pylibmc.py
15
pylibmc.py
@ -22,6 +22,7 @@ minor differences. If you should happen to spot any, file a bug!
|
|||||||
|
|
||||||
import _pylibmc
|
import _pylibmc
|
||||||
from warnings import warn
|
from warnings import warn
|
||||||
|
from Queue import Queue
|
||||||
|
|
||||||
__all__ = ["hashers", "distributions", "Client"]
|
__all__ = ["hashers", "distributions", "Client"]
|
||||||
__version__ = _pylibmc.__version__
|
__version__ = _pylibmc.__version__
|
||||||
@ -135,7 +136,7 @@ class Client(_pylibmc.client):
|
|||||||
|
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
|
|
||||||
class ClientPool(list):
|
class ClientPool(Queue):
|
||||||
"""Client pooling helper.
|
"""Client pooling helper.
|
||||||
|
|
||||||
This is mostly useful in threaded environments, because a client isn't
|
This is mostly useful in threaded environments, because a client isn't
|
||||||
@ -155,19 +156,23 @@ class ClientPool(list):
|
|||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
def __init__(self, mc, n_slots):
|
||||||
|
Queue.__init__(self, n_slots)
|
||||||
|
self.fill(mc, n_slots)
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def reserve(self):
|
def reserve(self, timeout=None):
|
||||||
"""Reserve a client, and put it back into the pool when done."""
|
"""Reserve a client, and put it back into the pool when done."""
|
||||||
mc = self.pop()
|
mc = self.get(True, timeout=timeout)
|
||||||
try:
|
try:
|
||||||
yield mc
|
yield mc
|
||||||
finally:
|
finally:
|
||||||
self.append(mc)
|
self.put(mc)
|
||||||
|
|
||||||
def fill(self, mc, n_slots):
|
def fill(self, mc, n_slots):
|
||||||
"""Fill *n_slots* of the pool with clones of *mc*."""
|
"""Fill *n_slots* of the pool with clones of *mc*."""
|
||||||
for i in xrange(n_slots):
|
for i in xrange(n_slots):
|
||||||
self.append(mc.clone())
|
self.put(mc.clone())
|
||||||
|
|
||||||
class ThreadMappedPool(dict):
|
class ThreadMappedPool(dict):
|
||||||
"""Much like the *ClientPool*, helps you with pooling.
|
"""Much like the *ClientPool*, helps you with pooling.
|
||||||
|
Reference in New Issue
Block a user