Commit 74b97b90 authored by Ron Rothman's avatar Ron Rothman Committed by Jason Madden

Add blocking and timeout params to Pool.add

parent 387b4f3c
...@@ -461,10 +461,13 @@ class Group(GroupMappingMixin): ...@@ -461,10 +461,13 @@ class Group(GroupMappingMixin):
""" """
return iter(self.greenlets) return iter(self.greenlets)
def add(self, greenlet): def add(self, greenlet, blocking=False, timeout=None):
""" """
Begin tracking the greenlet. Begin tracking the greenlet.
:keyword bool blocking: and :keyword bool timeout: are ignored
in this method; subclasses may use them (see :meth:`Pool.add`).
If this group is :meth:`full`, then this method may block If this group is :meth:`full`, then this method may block
until it is possible to track the greenlet. until it is possible to track the greenlet.
""" """
...@@ -714,19 +717,39 @@ class Pool(Group): ...@@ -714,19 +717,39 @@ class Pool(Group):
return 1 return 1
return max(0, self.size - len(self)) return max(0, self.size - len(self))
def add(self, greenlet): def add(self, greenlet, blocking=True, timeout=None):
""" """
Begin tracking the given greenlet, blocking until space is available. Begin tracking the given greenlet, possibly blocking until space is
available.
:keyword bool blocking: If True (the default), this function will block
until the pool has space or a timeout occurs.
:keyword float timeout: The maximum number of seconds this method will block.
:return: True if the greenlet was added; False if a Timeout occured or
if blocking is True and the pool is full.
.. seealso:: :meth:`Group.add` .. seealso:: :meth:`Group.add`
""" """
self._semaphore.acquire() try:
# NOTE: The docs for Semaphore.acquire indicate that it may raise a Timeout rather
# than return False under some circumstances, though I'm not sure exactly what those
# circumstances are.
was_acquired = self._semaphore.acquire(blocking=blocking, timeout=timeout)
except Timeout:
was_acquired = False
if not was_acquired:
return False
try: try:
Group.add(self, greenlet) Group.add(self, greenlet)
except: except:
self._semaphore.release() self._semaphore.release()
raise raise
return True
def _discard(self, greenlet): def _discard(self, greenlet):
Group._discard(self, greenlet) Group._discard(self, greenlet)
self._semaphore.release() self._semaphore.release()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment