How to use the mitogen.service function in mitogen

To help you get started, we’ve selected a few mitogen examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dw / mitogen / tests / bench / service.py View on Github external
"""
Measure latency of local service RPC.
"""

import time

import mitogen
import mitogen.core
import mitogen.service


class MyService(mitogen.service.Service):
    @mitogen.service.expose(policy=mitogen.service.AllowParents())
    def ping(self):
        return 'pong'


@mitogen.main()
def main(router):
    f = router.fork()
    t0 = mitogen.core.now()
    for x in range(1000):
        f.call_service(service_name=MyService, method_name='ping')
    print('++', int(1e6 * ((mitogen.core.now() - t0) / (1.0+x))), 'usec')
github dw / mitogen / tests / bench / throughput.py View on Github external
def transfer(context, path):
    fp = open('/dev/null', 'wb')
    mitogen.service.FileService.get(context, path, fp)
    fp.close()
github opsmop / opsmop / opsmop / providers / provider.py View on Github external
def copy_file(self, src, dest):
        """
        Copy a file in local mode, or download from the fileserver in push mode
        """
        caller = Context().caller()
        if caller:
            bio = open(dest, "wb", buffering=0)     
            if not src.startswith('/'):    
                src = os.path.join(Context().relative_root(), src)
            ok, metadata = mitogen.service.FileService.get(caller, src, bio)
            if not ok:
               raise Exception("file transfer failed")
        else:
            shutil.copy2(src, dest)
github dw / mitogen / preamble_size.py View on Github external
'                           '
    ' '
    '  Original   '
    '  '
    '     Minimized     '
    '  '
    '    Compressed     '
)

for mod in (
        mitogen.parent,
        mitogen.fork,
        mitogen.ssh,
        mitogen.sudo,
        mitogen.select,
        mitogen.service,
        mitogen.fakessh,
        mitogen.master,
    ):
    original = inspect.getsource(mod)
    original_size = len(original)
    minimized = mitogen.minify.minimize_source(original)
    minimized_size = len(minimized)
    compressed = zlib.compress(minimized, 9)
    compressed_size = len(compressed)
    print(
        '%-25s'
        ' '
        '%5i %4.1fKiB'
        '  '
        '%5i %4.1fKiB %.1f%%'
        '  '
github dw / mitogen / ansible_mitogen / plugins / actions / mitogen_async_status.py View on Github external
def _get_async_result(self, job_id):
        self._connection._connect()
        return mitogen.service.call(
            context=self._connection.parent,
            handle=ansible_mitogen.services.JobResultService.handle,
            method='get',
            kwargs={
                'job_id': job_id,
            }
github dw / mitogen / examples / service / server.py View on Github external
# The service framework will fundamentally change (i.e. become much nicer, and
# hopefully lose those hard-coded magic numbers somehow), but meanwhile this is
# a taster of how it looks today.

import mitogen
import mitogen.service
import mitogen.unix


class PingService(mitogen.service.Service):
    def dispatch(self, dct, msg):
        return 'Hello, world'


@mitogen.main()
def main(router):
    listener = mitogen.unix.Listener(router, path='/tmp/mitosock')
    service = PingService(router)
    service.run()
github dw / mitogen / mitogen / core.py View on Github external
def _service_stub_main(self, msg):
        self.service_stub_lock.acquire()
        try:
            import mitogen.service
            pool = mitogen.service.get_or_create_pool(router=self.router)
            pool._receiver._on_receive(msg)
        finally:
            self.service_stub_lock.release()
github dw / mitogen / ansible_mitogen / process.py View on Github external
def setup_pool(pool):
    """
    Configure a connection multiplexer's :class:`mitogen.service.Pool` with
    services accessed by clients and WorkerProcesses.
    """
    pool.add(mitogen.service.FileService(router=pool.router))
    pool.add(mitogen.service.PushFileService(router=pool.router))
    pool.add(ansible_mitogen.services.ContextService(router=pool.router))
    pool.add(ansible_mitogen.services.ModuleDepService(pool.router))
    LOG.debug('Service pool configured: size=%d', pool.size)
github dw / mitogen / examples / the_basics.py View on Github external
print("Upload /tmp/blah via function call: %s" % (
        context.call(put_file_contents, '/tmp/blah', b'blah!'),
    ))

    # Now lets transfer what might be a big files. The problem with big files
    # is that they may not fit in RAM. This uses mitogen.services.FileService
    # to implement streamy file transfer instead. The sender must have a
    # 'service pool' running that will host FileService. First let's do the
    # 'upload' direction, where the master hosts FileService.

    # Steals the 'Router' reference from the context object. In a real app the
    # pool would be constructed once at startup, this is just demo code.
    file_service = mitogen.service.FileService(context.router)

    # Start the pool.
    pool = mitogen.service.Pool(context.router, services=[file_service])

    # Grant access to a file on the local disk from unprivileged contexts.
    # .register() is also exposed as a service method -- you can call it on a
    # child context from any more privileged context.
    file_service.register('/etc/passwd')

    # Now call our wrapper function that knows how to handle the transfer. In a
    # real app, this wrapper might also set ownership/modes or do any other
    # app-specific stuff relating to the file that was transferred.
    print("Streamy upload /etc/passwd: remote result: %s" % (
        context.call(
            streamy_download_file,
            # To avoid hard-wiring streamy_download_file(), we want to pass it
            # a Context object that hosts the file service it should request
            # files from. Router.myself() returns a Context referring to this
            # process.
github dw / mitogen / ansible_mitogen / planner.py View on Github external
def get_module_map(self):
        if self._module_map is None:
            binding = self._inv.connection.get_binding()
            self._module_map = mitogen.service.call(
                call_context=binding.get_service_context(),
                service_name='ansible_mitogen.services.ModuleDepService',
                method_name='scan',

                module_name='ansible_module_%s' % (self._inv.module_name,),
                module_path=self._inv.module_path,
                search_path=self.get_search_path(),
                builtin_path=module_common._MODULE_UTILS_PATH,
                context=self._inv.connection.context,
            )
        return self._module_map