2018-03-23 03:46:11 +00:00
|
|
|
|
|
|
|
.. currentmodule:: mitogen.service
|
|
|
|
|
2018-07-09 01:42:50 +00:00
|
|
|
.. _service:
|
2018-03-23 03:46:11 +00:00
|
|
|
|
|
|
|
Service Framework
|
|
|
|
=================
|
|
|
|
|
2018-07-09 02:05:48 +00:00
|
|
|
.. warning::
|
|
|
|
|
|
|
|
This section is incomplete.
|
|
|
|
|
2018-03-23 03:46:11 +00:00
|
|
|
Mitogen includes a simple framework for implementing services exposed to other
|
2018-05-28 04:09:19 +00:00
|
|
|
contexts, with some built-in subclasses to capture common designs. This is a
|
|
|
|
work in progress, and new functionality will be added as common usage patterns
|
|
|
|
emerge.
|
2018-03-23 03:46:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
Overview
|
|
|
|
--------
|
|
|
|
|
2018-05-28 04:09:19 +00:00
|
|
|
Service
|
|
|
|
|
|
|
|
* User-supplied class with explicitly exposed methods.
|
|
|
|
* May be auto-imported/constructed in a child from a parent simply by calling it
|
2018-06-07 15:40:50 +00:00
|
|
|
* Identified in calls by its canonical name (e.g. mypkg.mymod.MyClass) by
|
|
|
|
default, but may use any naming scheme the configured activator understands.
|
2018-05-28 04:09:19 +00:00
|
|
|
* Children receive refusals if the class is not already activated by a aprent
|
|
|
|
* Has an associated Select instance which may be dynamically loaded with
|
|
|
|
receivers over time, on_message_received() invoked if any receiver becomes
|
|
|
|
ready.
|
|
|
|
|
|
|
|
Invoker
|
|
|
|
|
|
|
|
* Abstracts mechanism for calling a service method and verifying permissions.
|
|
|
|
* Built-in 'service.Invoker': concurrent execution of all methods on the thread pool.
|
2018-06-07 15:40:50 +00:00
|
|
|
* Built-in 'service.SerializedInvoker': serialization of all calls on a single
|
|
|
|
thread borrowed from the pool while any request is pending.
|
2018-05-28 04:09:19 +00:00
|
|
|
* Built-in 'service.DeduplicatingInvoker': requests are aggregated by distinct
|
2018-06-07 15:40:50 +00:00
|
|
|
(method, kwargs) key, only one such method ever executes, return value is
|
|
|
|
cached and broadcast to all request waiters. Waiters do not block additional
|
|
|
|
pool threads.
|
2018-05-28 04:09:19 +00:00
|
|
|
|
|
|
|
Activator
|
|
|
|
|
|
|
|
* Abstracts mechanism for activating a service and verifying activation
|
|
|
|
permission.
|
|
|
|
* Built-in activator looks for service by fully.qualified.ClassName using
|
|
|
|
Python import mechanism, and only permits parents to trigger activation.
|
|
|
|
|
|
|
|
Pool
|
|
|
|
|
|
|
|
* Manages a fixed-size thread pool, mapping of service name to Invoker, and an
|
|
|
|
aggregate Select over every activate service's Selects.
|
|
|
|
* Constructed automatically in children in response to the first
|
|
|
|
CALL_SERVICE message sent to them by a parent.
|
|
|
|
* Must be constructed manually in parent context.
|
|
|
|
* Has close() and add() methods.
|
|
|
|
|
2018-03-23 03:46:11 +00:00
|
|
|
|
|
|
|
Example
|
|
|
|
-------
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
import mitogen
|
|
|
|
import mitogen.service
|
|
|
|
|
|
|
|
|
|
|
|
class FileService(mitogen.service.Service):
|
|
|
|
"""
|
|
|
|
Simple file server, for demonstration purposes only! Use of this in
|
|
|
|
real code would be a security vulnerability as it would permit children
|
|
|
|
to read arbitrary files from the master's disk.
|
|
|
|
"""
|
|
|
|
handle = 500
|
|
|
|
required_args = {
|
|
|
|
'path': str
|
|
|
|
}
|
|
|
|
|
|
|
|
def dispatch(self, args, msg):
|
|
|
|
with open(args['path'], 'r') as fp:
|
|
|
|
return fp.read()
|
|
|
|
|
|
|
|
|
|
|
|
def download_file(context, path):
|
|
|
|
s = mitogen.service.call(context, FileService.handle, {
|
|
|
|
'path': path
|
|
|
|
})
|
|
|
|
|
|
|
|
with open(path, 'w') as fp:
|
|
|
|
fp.write(s)
|
|
|
|
|
|
|
|
|
|
|
|
@mitogen.core.takes_econtext
|
|
|
|
def download_some_files(paths, econtext):
|
|
|
|
for path in paths:
|
|
|
|
download_file(econtext.master, path)
|
|
|
|
|
|
|
|
|
|
|
|
@mitogen.main()
|
|
|
|
def main(router):
|
|
|
|
pool = mitogen.service.Pool(router, size=1, services=[
|
|
|
|
FileService(router),
|
|
|
|
])
|
|
|
|
|
|
|
|
remote = router.ssh(hostname='k3')
|
|
|
|
remote.call(download_some_files, [
|
|
|
|
'/etc/passwd',
|
|
|
|
'/etc/hosts',
|
|
|
|
])
|
|
|
|
pool.stop()
|
|
|
|
|
|
|
|
|
|
|
|
Reference
|
|
|
|
---------
|
|
|
|
|
2018-04-08 22:50:57 +00:00
|
|
|
.. autoclass:: mitogen.service.Policy
|
|
|
|
.. autoclass:: mitogen.service.AllowParents
|
|
|
|
.. autoclass:: mitogen.service.AllowAny
|
|
|
|
|
|
|
|
.. autofunction:: mitogen.service.arg_spec
|
|
|
|
.. autofunction:: mitogen.service.expose
|
|
|
|
|
2018-03-23 03:46:11 +00:00
|
|
|
.. autofunction:: mitogen.service.Service
|
|
|
|
|
2018-07-08 21:00:00 +00:00
|
|
|
.. autoclass:: mitogen.service.Invoker
|
|
|
|
.. autoclass:: mitogen.service.SerializedInvoker
|
|
|
|
.. autoclass:: mitogen.service.DeduplicatingInvoker
|
2018-03-23 03:46:11 +00:00
|
|
|
|
2018-07-08 21:00:00 +00:00
|
|
|
.. autoclass:: mitogen.service.Service
|
2018-03-23 03:46:11 +00:00
|
|
|
:members:
|
|
|
|
|
|
|
|
.. autoclass:: mitogen.service.Pool
|
|
|
|
:members:
|
|
|
|
|