whitespace and linelen cleanup

This commit is contained in:
Tavis Rudd
2010-02-27 01:21:16 -05:00
parent a3fa57c0ef
commit 28a2d4944c

View File

@@ -7,15 +7,15 @@ class Semaphore(object):
:meth:`release` resources as needed. Attempting to :meth:`acquire` when :meth:`release` resources as needed. Attempting to :meth:`acquire` when
*count* is zero suspends the calling greenthread until *count* becomes *count* is zero suspends the calling greenthread until *count* becomes
nonzero again. nonzero again.
This is API-compatible with :class:`threading.Semaphore`. This is API-compatible with :class:`threading.Semaphore`.
It is a context manager, and thus can be used in a with block:: It is a context manager, and thus can be used in a with block::
sem = Semaphore(2) sem = Semaphore(2)
with sem: with sem:
do_some_stuff() do_some_stuff()
If not specified, *value* defaults to 1. If not specified, *value* defaults to 1.
""" """
@@ -27,7 +27,8 @@ class Semaphore(object):
self._waiters = set() self._waiters = set()
def __repr__(self): def __repr__(self):
params = (self.__class__.__name__, hex(id(self)), self.counter, len(self._waiters)) params = (self.__class__.__name__, hex(id(self)),
self.counter, len(self._waiters))
return '<%s at %s c=%s _w[%s]>' % params return '<%s at %s c=%s _w[%s]>' % params
def __str__(self): def __str__(self):
@@ -35,30 +36,31 @@ class Semaphore(object):
return '<%s c=%s _w[%s]>' % params return '<%s c=%s _w[%s]>' % params
def locked(self): def locked(self):
""" Returns true if a call to acquire would block.""" """Returns true if a call to acquire would block."""
return self.counter <= 0 return self.counter <= 0
def bounded(self): def bounded(self):
""" Returns False; for consistency with :class:`~eventlet.semaphore.CappedSemaphore`.""" """Returns False; for consistency with
:class:`~eventlet.semaphore.CappedSemaphore`."""
return False return False
def acquire(self, blocking=True): def acquire(self, blocking=True):
"""Acquire a semaphore. """Acquire a semaphore.
When invoked without arguments: if the internal counter is larger than When invoked without arguments: if the internal counter is larger than
zero on entry, decrement it by one and return immediately. If it is zero zero on entry, decrement it by one and return immediately. If it is zero
on entry, block, waiting until some other thread has called release() to on entry, block, waiting until some other thread has called release() to
make it larger than zero. This is done with proper interlocking so that make it larger than zero. This is done with proper interlocking so that
if multiple acquire() calls are blocked, release() will wake exactly one if multiple acquire() calls are blocked, release() will wake exactly one
of them up. The implementation may pick one at random, so the order in of them up. The implementation may pick one at random, so the order in
which blocked threads are awakened should not be relied on. There is no which blocked threads are awakened should not be relied on. There is no
return value in this case. return value in this case.
When invoked with blocking set to true, do the same thing as when called When invoked with blocking set to true, do the same thing as when called
without arguments, and return true. without arguments, and return true.
When invoked with blocking set to false, do not block. If a call without When invoked with blocking set to false, do not block. If a call without
an argument would block, return false immediately; otherwise, do the an argument would block, return false immediately; otherwise, do the
same thing as when called without arguments, and return true.""" same thing as when called without arguments, and return true."""
if not blocking and self.locked(): if not blocking and self.locked():
return False return False
@@ -76,11 +78,11 @@ class Semaphore(object):
self.acquire() self.acquire()
def release(self, blocking=True): def release(self, blocking=True):
"""Release a semaphore, incrementing the internal counter by one. When """Release a semaphore, incrementing the internal counter by one. When
it was zero on entry and another thread is waiting for it to become it was zero on entry and another thread is waiting for it to become
larger than zero again, wake up that thread. larger than zero again, wake up that thread.
The *blocking* argument is for consistency with CappedSemaphore and is The *blocking* argument is for consistency with CappedSemaphore and is
ignored""" ignored"""
self.counter += 1 self.counter += 1
if self._waiters: if self._waiters:
@@ -98,12 +100,12 @@ class Semaphore(object):
@property @property
def balance(self): def balance(self):
"""An integer value that represents how many new calls to """An integer value that represents how many new calls to
:meth:`acquire` or :meth:`release` would be needed to get the counter to :meth:`acquire` or :meth:`release` would be needed to get the counter to
0. If it is positive, then its value is the number of acquires that can 0. If it is positive, then its value is the number of acquires that can
happen before the next acquire would block. If it is negative, it is happen before the next acquire would block. If it is negative, it is
the negative of the number of releases that would be required in order the negative of the number of releases that would be required in order
to make the counter 0 again (one more release would push the counter to to make the counter 0 again (one more release would push the counter to
1 and unblock acquirers). It takes into account how many greenthreads 1 and unblock acquirers). It takes into account how many greenthreads
are currently blocking in :meth:`acquire`. are currently blocking in :meth:`acquire`.
""" """
# positive means there are free items # positive means there are free items
@@ -113,45 +115,45 @@ class Semaphore(object):
class BoundedSemaphore(Semaphore): class BoundedSemaphore(Semaphore):
"""A bounded semaphore checks to make sure its current value doesn't exceed """A bounded semaphore checks to make sure its current value doesn't exceed
its initial value. If it does, ValueError is raised. In most situations its initial value. If it does, ValueError is raised. In most situations
semaphores are used to guard resources with limited capacity. If the semaphores are used to guard resources with limited capacity. If the
semaphore is released too many times it's a sign of a bug. If not given, semaphore is released too many times it's a sign of a bug. If not given,
*value* defaults to 1.""" *value* defaults to 1."""
def __init__(self, value=1): def __init__(self, value=1):
super(BoundedSemaphore, self).__init__(value) super(BoundedSemaphore, self).__init__(value)
self.original_counter = value self.original_counter = value
def release(self, blocking=True): def release(self, blocking=True):
"""Release a semaphore, incrementing the internal counter by one. If """Release a semaphore, incrementing the internal counter by one. If
the counter would exceed the initial value, raises ValueError. When the counter would exceed the initial value, raises ValueError. When
it was zero on entry and another thread is waiting for it to become it was zero on entry and another thread is waiting for it to become
larger than zero again, wake up that thread. larger than zero again, wake up that thread.
The *blocking* argument is for consistency with :class:`CappedSemaphore` The *blocking* argument is for consistency with :class:`CappedSemaphore`
and is ignored""" and is ignored"""
if self.counter >= self.original_counter: if self.counter >= self.original_counter:
raise ValueError, "Semaphore released too many times" raise ValueError, "Semaphore released too many times"
return super(BoundedSemaphore, self).release(blocking) return super(BoundedSemaphore, self).release(blocking)
class CappedSemaphore(object): class CappedSemaphore(object):
"""A blockingly bounded semaphore. """A blockingly bounded semaphore.
Optionally initialize with a resource *count*, then :meth:`acquire` and Optionally initialize with a resource *count*, then :meth:`acquire` and
:meth:`release` resources as needed. Attempting to :meth:`acquire` when :meth:`release` resources as needed. Attempting to :meth:`acquire` when
*count* is zero suspends the calling greenthread until count becomes nonzero *count* is zero suspends the calling greenthread until count becomes nonzero
again. Attempting to :meth:`release` after *count* has reached *limit* again. Attempting to :meth:`release` after *count* has reached *limit*
suspends the calling greenthread until *count* becomes less than *limit* suspends the calling greenthread until *count* becomes less than *limit*
again. again.
This has the same API as :class:`threading.Semaphore`, though its This has the same API as :class:`threading.Semaphore`, though its
semantics and behavior differ subtly due to the upper limit on calls semantics and behavior differ subtly due to the upper limit on calls
to :meth:`release`. It is **not** compatible with to :meth:`release`. It is **not** compatible with
:class:`threading.BoundedSemaphore` because it blocks when reaching *limit* :class:`threading.BoundedSemaphore` because it blocks when reaching *limit*
instead of raising a ValueError. instead of raising a ValueError.
It is a context manager, and thus can be used in a with block:: It is a context manager, and thus can be used in a with block::
sem = CappedSemaphore(2) sem = CappedSemaphore(2)
with sem: with sem:
do_some_stuff() do_some_stuff()
@@ -167,38 +169,40 @@ class CappedSemaphore(object):
self.upper_bound = Semaphore(limit-count) self.upper_bound = Semaphore(limit-count)
def __repr__(self): def __repr__(self):
params = (self.__class__.__name__, hex(id(self)), self.balance, self.lower_bound, self.upper_bound) params = (self.__class__.__name__, hex(id(self)),
self.balance, self.lower_bound, self.upper_bound)
return '<%s at %s b=%s l=%s u=%s>' % params return '<%s at %s b=%s l=%s u=%s>' % params
def __str__(self): def __str__(self):
params = (self.__class__.__name__, self.balance, self.lower_bound, self.upper_bound) params = (self.__class__.__name__, self.balance,
self.lower_bound, self.upper_bound)
return '<%s b=%s l=%s u=%s>' % params return '<%s b=%s l=%s u=%s>' % params
def locked(self): def locked(self):
"""Returns true if a call to acquire would block.""" """Returns true if a call to acquire would block."""
return self.lower_bound.locked() return self.lower_bound.locked()
def bounded(self): def bounded(self):
"""Returns true if a call to release would block.""" """Returns true if a call to release would block."""
return self.upper_bound.locked() return self.upper_bound.locked()
def acquire(self, blocking=True): def acquire(self, blocking=True):
"""Acquire a semaphore. """Acquire a semaphore.
When invoked without arguments: if the internal counter is larger than When invoked without arguments: if the internal counter is larger than
zero on entry, decrement it by one and return immediately. If it is zero zero on entry, decrement it by one and return immediately. If it is zero
on entry, block, waiting until some other thread has called release() to on entry, block, waiting until some other thread has called release() to
make it larger than zero. This is done with proper interlocking so that make it larger than zero. This is done with proper interlocking so that
if multiple acquire() calls are blocked, release() will wake exactly one if multiple acquire() calls are blocked, release() will wake exactly one
of them up. The implementation may pick one at random, so the order in of them up. The implementation may pick one at random, so the order in
which blocked threads are awakened should not be relied on. There is no which blocked threads are awakened should not be relied on. There is no
return value in this case. return value in this case.
When invoked with blocking set to true, do the same thing as when called When invoked with blocking set to true, do the same thing as when called
without arguments, and return true. without arguments, and return true.
When invoked with blocking set to false, do not block. If a call without When invoked with blocking set to false, do not block. If a call without
an argument would block, return false immediately; otherwise, do the an argument would block, return false immediately; otherwise, do the
same thing as when called without arguments, and return true.""" same thing as when called without arguments, and return true."""
if not blocking and self.locked(): if not blocking and self.locked():
return False return False
@@ -218,7 +222,7 @@ class CappedSemaphore(object):
def release(self, blocking=True): def release(self, blocking=True):
"""Release a semaphore. In this class, this behaves very much like """Release a semaphore. In this class, this behaves very much like
an :meth:`acquire` but in the opposite direction. an :meth:`acquire` but in the opposite direction.
Imagine the docs of :meth:`acquire` here, but with every direction Imagine the docs of :meth:`acquire` here, but with every direction
reversed. When calling this method, it will block if the internal reversed. When calling this method, it will block if the internal
counter is greater than or equal to *limit*.""" counter is greater than or equal to *limit*."""
@@ -237,11 +241,11 @@ class CappedSemaphore(object):
@property @property
def balance(self): def balance(self):
"""An integer value that represents how many new calls to """An integer value that represents how many new calls to
:meth:`acquire` or :meth:`release` would be needed to get the counter to :meth:`acquire` or :meth:`release` would be needed to get the counter to
0. If it is positive, then its value is the number of acquires that can 0. If it is positive, then its value is the number of acquires that can
happen before the next acquire would block. If it is negative, it is happen before the next acquire would block. If it is negative, it is
the negative of the number of releases that would be required in order the negative of the number of releases that would be required in order
to make the counter 0 again (one more release would push the counter to to make the counter 0 again (one more release would push the counter to
1 and unblock acquirers). It takes into account how many greenthreads 1 and unblock acquirers). It takes into account how many greenthreads
are currently blocking in :meth:`acquire` and :meth:`release`.""" are currently blocking in :meth:`acquire` and :meth:`release`."""
return self.lower_bound.balance - self.upper_bound.balance return self.lower_bound.balance - self.upper_bound.balance