Tidy up select docs.
This commit is contained in:
parent
3c288d12b0
commit
265d9f0293
43
docs/api.rst
43
docs/api.rst
|
@ -68,9 +68,8 @@ contexts.
|
||||||
instances and returns the first value posted to any receiver or select.
|
instances and returns the first value posted to any receiver or select.
|
||||||
|
|
||||||
If `oneshot` is ``True``, then remove each receiver as it yields a result;
|
If `oneshot` is ``True``, then remove each receiver as it yields a result;
|
||||||
since :py:meth:`__iter__` terminates once the final receiver is removed
|
since :py:meth:`__iter__` terminates once the final receiver is removed,
|
||||||
from the select, this makes it convenient to respond to several call
|
this makes it convenient to respond to calls made in parallel:
|
||||||
results with minimal effort:
|
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
|
@ -78,26 +77,25 @@ contexts.
|
||||||
recvs = [c.call_async(long_running_operation) for c in contexts]
|
recvs = [c.call_async(long_running_operation) for c in contexts]
|
||||||
|
|
||||||
with mitogen.master.Select(recvs) as select:
|
with mitogen.master.Select(recvs) as select:
|
||||||
for recv, msg in select:
|
for recv, (msg, data) in select:
|
||||||
value = msg.unpickle()
|
print 'Got %s from %s' % (data, recv)
|
||||||
print 'Got %s from %s' % (value, recv)
|
total += data
|
||||||
total += value
|
|
||||||
|
|
||||||
# Iteration ends when last Receiver yields a result.
|
# Iteration ends when last Receiver yields a result.
|
||||||
print 'Received total %s from %s receivers' % (total, len(recvs))
|
print 'Received total %s from %s receivers' % (total, len(recvs))
|
||||||
|
|
||||||
:py:class:`Select` may also be used to drive a long-running scheduler:
|
:py:class:`Select` may drive a long-running scheduler:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
with mitogen.master.Select() as select:
|
with mitogen.master.Select(oneshot=False) as select:
|
||||||
while running():
|
while running():
|
||||||
for recv, msg in select:
|
for recv, (msg, data) in select:
|
||||||
process_result(recv.context, msg.unpickle())
|
process_result(recv.context, msg.unpickle())
|
||||||
for context, workfunc in get_new_work():
|
for context, workfunc in get_new_work():
|
||||||
select.add(context.call_async(workfunc))
|
select.add(context.call_async(workfunc))
|
||||||
|
|
||||||
:py:class:`Select` may be arbitrarily nested:
|
:py:class:`Select` may be nested:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
|
@ -110,9 +108,9 @@ contexts.
|
||||||
])
|
])
|
||||||
]
|
]
|
||||||
|
|
||||||
with mitogen.master.Select(selects, oneshot=False) as select:
|
with mitogen.master.Select(selects) as select:
|
||||||
while subselects and any(subselects): # Calls __bool__()
|
for _, (msg, data) in select:
|
||||||
print select.get()
|
print data
|
||||||
|
|
||||||
.. py:method:: get (timeout=None)
|
.. py:method:: get (timeout=None)
|
||||||
|
|
||||||
|
@ -138,17 +136,16 @@ contexts.
|
||||||
|
|
||||||
.. py:method:: empty ()
|
.. py:method:: empty ()
|
||||||
|
|
||||||
Return ``True`` if no items appear to be queued on this receiver.
|
Return ``True`` if calling :py:meth:`get` would block.
|
||||||
|
|
||||||
As with :py:class:`Queue.Queue`, this function may return ``False``
|
As with :py:class:`Queue.Queue`, ``True`` may be returned even though a
|
||||||
even though a subsequent call to :py:meth:`get` will succeed, since a
|
subsequent call to :py:meth:`get` will succeed, since a message may be
|
||||||
message may be posted at any moment between the call to
|
posted at any moment between :py:meth:`empty` and :py:meth:`get`.
|
||||||
:py:meth:`empty` and :py:meth:`get`.
|
|
||||||
|
|
||||||
:py:meth:`empty` may additionally return ``True`` when :py:meth:`get`
|
:py:meth:`empty` may return ``False`` even when :py:meth:`get` would
|
||||||
would block if another thread has drained a receiver added to this
|
block if another thread has drained a receiver added to this select.
|
||||||
select. This can be avoided by only consuming each receiver from a
|
This can be avoided by only consuming each receiver from a single
|
||||||
single thread.
|
thread.
|
||||||
|
|
||||||
.. py:method:: __iter__ (self)
|
.. py:method:: __iter__ (self)
|
||||||
|
|
||||||
|
|
|
@ -173,8 +173,8 @@ for example enforce an interactive TTY and account password.
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|
||||||
Inter-slave Message Routing
|
Message Routing
|
||||||
###########################
|
###############
|
||||||
|
|
||||||
.. image:: images/route.png
|
.. image:: images/route.png
|
||||||
|
|
||||||
|
@ -255,8 +255,30 @@ and timeouts can be configured to ensure failed calls do not block progress of
|
||||||
the parent.
|
the parent.
|
||||||
|
|
||||||
|
|
||||||
Support For Single File Programs
|
Scatter/Gather Calls
|
||||||
################################
|
####################
|
||||||
|
|
||||||
|
Functions may be invoked asynchronously, with results returned as they become
|
||||||
|
available.
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
def usage(path):
|
||||||
|
return sum((os.path.getsize(os.path.join(dirpath, name))
|
||||||
|
for dirpath, dirnames, filenames in os.walk(path)
|
||||||
|
for name in dirnames + filenames), 0)
|
||||||
|
|
||||||
|
total = 0
|
||||||
|
for recv, msg in Select(c.call_async(usage, '/tmp') for c in contexts):
|
||||||
|
value = result.unpickle()
|
||||||
|
print 'Context %s /tmp usage: %d' % (recv.context, value)
|
||||||
|
total += value
|
||||||
|
|
||||||
|
print 'Total /tmp usage across all contexts: %d' % (total,)
|
||||||
|
|
||||||
|
|
||||||
|
Single File Programs
|
||||||
|
####################
|
||||||
|
|
||||||
Programs that are self-contained within a single Python script are supported.
|
Programs that are self-contained within a single Python script are supported.
|
||||||
External contexts are configured such that any attempt to execute a function
|
External contexts are configured such that any attempt to execute a function
|
||||||
|
@ -298,33 +320,6 @@ usual into the slave process.
|
||||||
mitogen.utils.run_with_broker(main)
|
mitogen.utils.run_with_broker(main)
|
||||||
|
|
||||||
|
|
||||||
Scatter/Gather Function Calls
|
|
||||||
#############################
|
|
||||||
|
|
||||||
Functions may be invoked asynchronously, with results returned as they become
|
|
||||||
available.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
def disk_usage(path):
|
|
||||||
return sum((os.path.getsize(os.path.join(dirpath, name))
|
|
||||||
for dirpath, dirnames, filenames in os.walk(path)
|
|
||||||
for name in dirnames + filenames), 0)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__' and mitogen.is_master:
|
|
||||||
contexts = connect_contexts(...)
|
|
||||||
receivers = [c.call_async(disk_usage, '/tmp') for c in contexts]
|
|
||||||
total = 0
|
|
||||||
|
|
||||||
for recv, msg in mitogen.master.Select(receivers):
|
|
||||||
value = result.unpickle()
|
|
||||||
print 'Context %s /tmp usage: %d' % (recv.context, value)
|
|
||||||
total += value
|
|
||||||
|
|
||||||
print 'Total /tmp usage across all contexts: %d' % (total,)
|
|
||||||
|
|
||||||
|
|
||||||
Event-driven IO
|
Event-driven IO
|
||||||
###############
|
###############
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue