Commit 561c4d90 authored by Ron Rothman's avatar Ron Rothman Committed by Jason Madden

PR comments:

    * revert Group.add to original state (no new params)
    * add new exception "Full", and return it when blocking==False and
    pool is full
    * no longer return True/False from Pool.add
parent 5cb91ae7
...@@ -27,7 +27,7 @@ from gevent.timeout import Timeout ...@@ -27,7 +27,7 @@ from gevent.timeout import Timeout
from gevent.event import Event from gevent.event import Event
from gevent.lock import Semaphore, DummySemaphore from gevent.lock import Semaphore, DummySemaphore
__all__ = ['Group', 'Pool'] __all__ = ['Group', 'Pool', 'Full']
class IMapUnordered(Greenlet): class IMapUnordered(Greenlet):
...@@ -461,13 +461,10 @@ class Group(GroupMappingMixin): ...@@ -461,13 +461,10 @@ class Group(GroupMappingMixin):
""" """
return iter(self.greenlets) return iter(self.greenlets)
def add(self, greenlet, blocking=False, timeout=None): def add(self, greenlet):
""" """
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.
""" """
...@@ -649,6 +646,14 @@ class Failure(object): ...@@ -649,6 +646,14 @@ class Failure(object):
raise self.exc raise self.exc
class Full(Exception):
"""
Raised when a Pool is full and an attempt was made to
add a new greenlet to it.
"""
pass
class Pool(Group): class Pool(Group):
def __init__(self, size=None, greenlet_class=None): def __init__(self, size=None, greenlet_class=None):
...@@ -723,24 +728,23 @@ class Pool(Group): ...@@ -723,24 +728,23 @@ class Pool(Group):
available. available.
:keyword bool blocking: If True (the default), this function will block :keyword bool blocking: If True (the default), this function will block
until the pool has space or a timeout occurs. until the pool has space or a timeout occurs. If False, this function
:keyword float timeout: The maximum number of seconds this method will block. will immediately raise a Timeout if the pool is currently full.
:keyword float timeout: The maximum number of seconds this method will
block, if ``blocking`` is True. (Ignored if ``blocking`` is False.)
:return: True if the greenlet was added; False if a Timeout occured or Raises ``Timeout`` on timeout, or `Full` if ``blocking`` is False and
if blocking is True and the pool is full. the pool is full.
.. seealso:: :meth:`Group.add` .. seealso:: :meth:`Group.add`
"""
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: .. versionchanged:: 1.3.0
return False Added the ``blocking`` and ```timeout`` parameters.
"""
if not self._semaphore.acquire(blocking=blocking, timeout=timeout):
# We failed to acquire the semaphore. Presumably, blocking was False, because had it
# been True, we would have either acquired the semaphore or encountered a Timeout.
raise Full
try: try:
Group.add(self, greenlet) Group.add(self, greenlet)
...@@ -748,8 +752,6 @@ class Pool(Group): ...@@ -748,8 +752,6 @@ class Pool(Group):
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