core/select: add {Select,Latch,Receiver}.size(), deprecate empty()

Knowing an estimate of the buffered items is needed for adding a
latch/receiver with many existing buffered items via Select.add().
pull/612/head
David Wilson 5 years ago
parent 95b067a114
commit 49a6446af8

@ -1095,13 +1095,32 @@ class Receiver(object):
self.handle = None self.handle = None
self._latch.close() self._latch.close()
def size(self):
"""
Return the number of items currently buffered.
As with :class:`Queue.Queue`, `0` may be returned even though a
subsequent call to :meth:`get` will succeed, since a message may be
posted at any moment between :meth:`size` and :meth:`get`.
As with :class:`Queue.Queue`, `>0` may be returned even though a
subsequent call to :meth:`get` will block, since another waiting thread
may be woken at any moment between :meth:`size` and :meth:`get`.
:raises LatchError:
The underlying latch has already been marked closed.
"""
return self._latch.size()
def empty(self): def empty(self):
""" """
Return :data:`True` if calling :meth:`get` would block. Return `size() == 0`.
.. deprecated:: 0.2.8
Use :meth:`size` instead.
As with :class:`Queue.Queue`, :data:`True` may be returned even though :raises LatchError:
a subsequent call to :meth:`get` will succeed, since a message may be The latch has already been marked closed.
posted at any moment between :meth:`empty` and :meth:`get`.
""" """
return self._latch.empty() return self._latch.empty()
@ -1150,7 +1169,10 @@ class Channel(Sender, Receiver):
A channel inherits from :class:`mitogen.core.Sender` and A channel inherits from :class:`mitogen.core.Sender` and
`mitogen.core.Receiver` to provide bidirectional functionality. `mitogen.core.Receiver` to provide bidirectional functionality.
This class is incomplete and obsolete, it will be removed in Mitogen 0.3. .. deprecated:: 0.2.0
This class is incomplete and obsolete, it will be removed in Mitogen
0.3.
Channels were an early attempt at syntax sugar. It is always easier to pass Channels were an early attempt at syntax sugar. It is always easier to pass
around unidirectional pairs of senders/receivers, even though the syntax is around unidirectional pairs of senders/receivers, even though the syntax is
baroque: baroque:
@ -2385,19 +2407,17 @@ class Latch(object):
finally: finally:
self._lock.release() self._lock.release()
def empty(self): def size(self):
""" """
Return :data:`True` if calling :meth:`get` would block. Return the number of items currently buffered.
As with :class:`Queue.Queue`, :data:`True` may be returned even As with :class:`Queue.Queue`, `0` may be returned even though a
though a subsequent call to :meth:`get` will succeed, since a subsequent call to :meth:`get` will succeed, since a message may be
message may be posted at any moment between :meth:`empty` and posted at any moment between :meth:`size` and :meth:`get`.
:meth:`get`.
As with :class:`Queue.Queue`, :data:`False` may be returned even As with :class:`Queue.Queue`, `>0` may be returned even though a
though a subsequent call to :meth:`get` will block, since another subsequent call to :meth:`get` will block, since another waiting thread
waiting thread may be woken at any moment between :meth:`empty` and may be woken at any moment between :meth:`size` and :meth:`get`.
:meth:`get`.
:raises LatchError: :raises LatchError:
The latch has already been marked closed. The latch has already been marked closed.
@ -2406,10 +2426,22 @@ class Latch(object):
try: try:
if self.closed: if self.closed:
raise LatchError() raise LatchError()
return len(self._queue) == 0 return len(self._queue)
finally: finally:
self._lock.release() self._lock.release()
def empty(self):
"""
Return `size() == 0`.
.. deprecated:: 0.2.8
Use :meth:`size` instead.
:raises LatchError:
The latch has already been marked closed.
"""
return self.size() == 0
def _get_socketpair(self): def _get_socketpair(self):
""" """
Return an unused socketpair, creating one if none exist. Return an unused socketpair, creating one if none exist.

@ -259,18 +259,26 @@ class Select(object):
self.remove(recv) self.remove(recv)
self._latch.close() self._latch.close()
def empty(self): def size(self):
"""
Return the number of items currently buffered.
As with :class:`Queue.Queue`, `0` may be returned even though a
subsequent call to :meth:`get` will succeed, since a message may be
posted at any moment between :meth:`size` and :meth:`get`.
As with :class:`Queue.Queue`, `>0` may be returned even though a
subsequent call to :meth:`get` will block, since another waiting thread
may be woken at any moment between :meth:`size` and :meth:`get`.
""" """
Return :data:`True` if calling :meth:`get` would block. return sum(recv.size() for recv in self._receivers)
As with :class:`Queue.Queue`, :data:`True` may be returned even though def empty(self):
a subsequent call to :meth:`get` will succeed, since a message may be """
posted at any moment between :meth:`empty` and :meth:`get`. Return `size() == 0`.
:meth:`empty` may return :data:`False` even when :meth:`get` would .. deprecated:: 0.2.8
block if another thread has drained a receiver added to this select. Use :meth:`size` instead.
This can be avoided by only consuming each receiver from a single
thread.
""" """
return self._latch.empty() return self._latch.empty()

Loading…
Cancel
Save