Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the pool lock to be specialized #41

Merged
merged 4 commits into from May 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 17 additions & 3 deletions pymemcache/client.py
Expand Up @@ -824,7 +824,19 @@ def __delitem__(self, key):


class PooledClient(object):
"""A thread-safe pool of clients (with the same client api)."""
"""A thread-safe pool of clients (with the same client api).

Args:
max_pool_size: maximum pool size to use (going about this amount
triggers a runtime error), by default this is 2147483648L
when not provided (or none).
lock_generator: a callback/type that takes no arguments that will
be called to create a lock or sempahore that can
protect the pool from concurrent access (for example a
eventlet lock or semaphore could be used instead)

Further arguments are interpreted as for :py:class:`.Client` constructor.
"""

def __init__(self,
server,
Expand All @@ -836,7 +848,8 @@ def __init__(self,
ignore_exc=False,
socket_module=socket,
key_prefix=b'',
max_pool_size=None):
max_pool_size=None,
lock_generator=None):
self.server = server
self.serializer = serializer
self.deserializer = deserializer
Expand All @@ -853,7 +866,8 @@ def __init__(self,
self.client_pool = pool.ObjectPool(
self._create_client,
after_remove=lambda client: client.close(),
max_size=max_pool_size)
max_size=max_pool_size,
lock_generator=lock_generator)

def check_key(self, key):
"""Checks key and add key_prefix."""
Expand Down
8 changes: 6 additions & 2 deletions pymemcache/pool.py
Expand Up @@ -24,11 +24,15 @@ class ObjectPool(object):
"""A pool of objects that release/creates/destroys as needed."""

def __init__(self, obj_creator,
after_remove=None, max_size=None):
after_remove=None, max_size=None,
lock_generator=None):
self._used_objs = collections.deque()
self._free_objs = collections.deque()
self._obj_creator = obj_creator
self._lock = threading.Lock()
if lock_generator is None:
self._lock = threading.Lock()
else:
self._lock = lock_generator()
self._after_remove = after_remove
max_size = max_size or 2 ** 31
if not isinstance(max_size, six.integer_types) or max_size < 0:
Expand Down