mitogen/docs/api.rst

247 lines
6.8 KiB
ReStructuredText
Raw Normal View History

2016-08-12 17:45:26 +00:00
API Reference
2016-08-13 23:16:17 +00:00
*************
Package Layout
==============
2017-09-11 04:08:28 +00:00
mitogen Package
---------------
2016-08-13 23:16:17 +00:00
2017-09-11 04:08:28 +00:00
.. automodule:: mitogen
2016-08-13 23:16:17 +00:00
.. autodata:: mitogen.is_master
2017-09-11 04:08:28 +00:00
.. autodata:: mitogen.context_id
.. autodata:: mitogen.parent_id
2017-09-07 11:08:05 +00:00
2016-08-12 17:45:26 +00:00
2017-09-11 04:08:28 +00:00
mitogen.core
------------
2016-08-12 17:45:26 +00:00
2017-09-18 09:20:41 +00:00
.. module:: mitogen.core
This module implements most package functionality, but remains separate from
non-essential code in order to reduce its size, since it is also serves as the
bootstrap implementation sent to every new slave context.
2016-08-13 23:16:17 +00:00
.. function:: mitogen.core.takes_econtext
Decorator that marks a function or class method to automatically receive a
kwarg named `econtext`, referencing the
:py:class:`econtext.core.ExternalContext` active in the context in which
the function is being invoked in. The decorator is only meaningful when the
function is invoked via :py:data:`econtext.core.CALL_FUNCTION`.
When the function is invoked directly, `econtext` must still be passed to it
explicitly.
.. function:: mitogen.core.takes_router
Decorator that marks a function or class method to automatically receive a
kwarg named `router`, referencing the :py:class:`econtext.core.Router`
active in the context in which the function is being invoked in. The
decorator is only meaningful when the function is invoked via
:py:data:`econtext.core.CALL_FUNCTION`.
When the function is invoked directly, `router` must still be passed to it
explicitly.
2016-08-13 23:16:17 +00:00
2017-09-11 04:08:28 +00:00
mitogen.master
--------------
2016-08-13 23:16:17 +00:00
.. module:: mitogen.master
This module implements functionality required by master processes, such as
starting new contexts via SSH. Its size is also restricted, since it must
be sent to any context that will be used to establish additional child
contexts.
2016-08-13 23:16:17 +00:00
.. class:: mitogen.master.Select (receivers=(), oneshot=True)
Support scatter/gather asynchronous calls and waiting on multiple
receivers, channels, and sub-Selects. Accepts a sequence of
:py:class:`mitogen.core.Receiver` or :py:class:`mitogen.master.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;
2017-09-28 04:54:52 +00:00
since :py:meth:`__iter__` terminates once the final receiver is removed,
this makes it convenient to respond to calls made in parallel:
.. code-block:: python
total = 0
recvs = [c.call_async(long_running_operation) for c in contexts]
with mitogen.master.Select(recvs) as select:
2017-09-28 04:54:52 +00:00
for recv, (msg, data) in select:
print 'Got %s from %s' % (data, recv)
total += data
# Iteration ends when last Receiver yields a result.
print 'Received total %s from %s receivers' % (total, len(recvs))
2017-09-28 04:54:52 +00:00
:py:class:`Select` may drive a long-running scheduler:
.. code-block:: python
2017-09-28 04:54:52 +00:00
with mitogen.master.Select(oneshot=False) as select:
while running():
2017-09-28 04:54:52 +00:00
for recv, (msg, data) in select:
process_result(recv.context, msg.unpickle())
for context, workfunc in get_new_work():
select.add(context.call_async(workfunc))
2017-09-28 04:54:52 +00:00
:py:class:`Select` may be nested:
.. code-block:: python
subselects = [
mitogen.master.Select(get_some_work()),
mitogen.master.Select(get_some_work()),
mitogen.master.Select([
mitogen.master.Select(get_some_work()),
mitogen.master.Select(get_some_work())
])
]
2017-09-28 04:54:52 +00:00
with mitogen.master.Select(selects) as select:
for _, (msg, data) in select:
print data
.. py:method:: get (timeout=None)
Fetch the next available value from any receiver, or raise
:py:class:`mitogen.core.TimeoutError` if no value is available within
`timeout` seconds.
:param float timeout:
Timeout in seconds.
:return:
2017-09-27 08:22:05 +00:00
`(receiver, (msg, data))`
.. py:method:: __bool__ ()
Return ``True`` if any receivers are registered with this select.
.. py:method:: close ()
Remove the select's notifier function from each registered receiver.
Necessary to prevent memory leaks in long-running receivers. This is
called automatically when the Python ``with:`` statement is used.
.. py:method:: empty ()
2017-09-28 04:54:52 +00:00
Return ``True`` if calling :py:meth:`get` would block.
2017-09-28 04:54:52 +00:00
As with :py:class:`Queue.Queue`, ``True`` may be returned even though a
subsequent call to :py:meth:`get` will succeed, since a message may be
posted at any moment between :py:meth:`empty` and :py:meth:`get`.
2017-09-28 04:54:52 +00:00
:py:meth:`empty` may return ``False`` even when :py:meth:`get` would
block if another thread has drained a receiver added to this select.
This can be avoided by only consuming each receiver from a single
thread.
.. py:method:: __iter__ (self)
Yield the result of :py:meth:`get` until no receivers remain in the
select, either because `oneshot` is ``True``, or each receiver was
explicitly removed via :py:meth:`remove`.
.. py:method:: add (recv)
Add the :py:class:`mitogen.core.Receiver` or
:py:class:`mitogen.core.Channel` `recv` to the select.
.. py:method:: remove (recv)
Remove the :py:class:`mitogen.core.Receiver` or
:py:class:`mitogen.core.Channel` `recv` from the select. Note that if
the receiver has notified prior to :py:meth:`remove`, then it will
still be returned by a subsequent :py:meth:`get`. This may change in a
future version.
2017-09-11 04:08:28 +00:00
mitogen.fakessh
2017-09-07 11:08:05 +00:00
---------------
2017-09-11 04:08:28 +00:00
.. automodule:: mitogen.fakessh
2017-09-11 04:08:28 +00:00
.. autofunction:: mitogen.fakessh.run
2017-09-07 11:08:05 +00:00
Router Class
============
2017-09-11 04:08:28 +00:00
.. autoclass:: mitogen.master.Router
2017-09-07 11:08:05 +00:00
:members:
:inherited-members:
Broker Class
============
2017-09-11 04:08:28 +00:00
.. autoclass:: mitogen.master.Broker
:members:
:inherited-members:
2016-08-13 23:16:17 +00:00
Context Class
=============
2016-08-13 23:16:17 +00:00
2017-09-11 04:08:28 +00:00
.. autoclass:: mitogen.master.Context
2016-08-13 23:16:17 +00:00
:members:
:inherited-members:
2016-08-13 23:16:17 +00:00
Channel Class
-------------
2017-09-11 04:08:28 +00:00
.. autoclass:: mitogen.core.Channel
2016-08-12 17:45:26 +00:00
:members:
Context Class
-------------
2016-08-13 23:16:17 +00:00
2017-09-11 04:08:28 +00:00
.. autoclass:: mitogen.master.Context
2016-08-13 23:16:17 +00:00
:members:
Utility Functions
=================
2016-08-13 23:16:17 +00:00
2017-09-11 04:08:28 +00:00
.. automodule:: mitogen.utils
2016-08-13 23:16:17 +00:00
:members:
Exceptions
==========
2017-09-18 09:20:41 +00:00
.. class:: mitogen.core.Error (fmt, \*args)
Base for all exceptions raised by Mitogen.
.. class:: mitogen.core.CallError (e)
Raised when :py:meth:`Context.call() <mitogen.master.Context.call>` fails.
A copy of the traceback from the external context is appended to the
exception message.
.. class:: mitogen.core.ChannelError (fmt, \*args)
Raised when a channel dies or has been closed.
.. class:: mitogen.core.StreamError (fmt, \*args)
Raised when a stream cannot be established.
.. autoclass:: mitogen.core.TimeoutError (fmt, \*args)
Raised when a timeout occurs on a stream.