How to use the mitogen.master.Router 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 / examples / mitogen-fuse.py View on Github external
def init(self, path):
        self.broker = mitogen.master.Broker(install_watcher=False)
        self.router = mitogen.master.Router(self.broker)
        self.host = self.router.ssh(hostname=self.host)
        self._context = self.router.sudo(via=self.host)
        #self._context.call(_chroot , '/home/dmw')
        self._stat_cache = {}
        self.ready.set()
github dw / mitogen / mitogen / master.py View on Github external
def _on_broker_exit(self):
        super(Router, self)._on_broker_exit()
        dct = self.get_stats()
        dct['self'] = self
        dct['minify_ms'] = 1000 * dct['minify_secs']
        dct['get_module_ms'] = 1000 * dct['get_module_secs']
        dct['good_load_module_size_kb'] = dct['good_load_module_size'] / 1024.0
        dct['good_load_module_size_avg'] = (
            (
                dct['good_load_module_size'] /
                (float(dct['good_load_module_count']) or 1.0)
            ) / 1024.0
        )

        LOG.debug(
            '%(self)r: stats: '
                '%(get_module_count)d module requests in '
                '%(get_module_ms)d ms, '
github dw / mitogen / preamble_size.py View on Github external
import inspect
import sys
import zlib

import mitogen.fakessh
import mitogen.fork
import mitogen.master
import mitogen.minify
import mitogen.parent
import mitogen.select
import mitogen.service
import mitogen.ssh
import mitogen.sudo

router = mitogen.master.Router()
context = mitogen.parent.Context(router, 0)
stream = mitogen.ssh.Stream(router, 0, max_message_size=0, hostname='foo')

print('SSH command size: %s' % (len(' '.join(stream.get_boot_command())),))
print('Preamble size: %s (%.2fKiB)' % (
    len(stream.get_preamble()),
    len(stream.get_preamble()) / 1024.0,
))
if '--dump' in sys.argv:
    print(zlib.decompress(stream.get_preamble()))
    exit()


print(
    '                           '
    ' '
github dw / mitogen / mitogen / utils.py View on Github external
def run_with_router(func, *args, **kwargs):
    """
    Arrange for `func(router, *args, **kwargs)` to run with a temporary
    :class:`mitogen.master.Router`, ensuring the Router and Broker are
    correctly shut down during normal or exceptional return.

    :returns:
        `func`'s return value.
    """
    broker = mitogen.master.Broker()
    router = mitogen.master.Router(broker)
    try:
        return func(router, *args, **kwargs)
    finally:
        broker.shutdown()
        broker.join()
github opsmop / opsmop / opsmop / push / connections.py View on Github external
def __init__(self, policy, limit_groups=None, limit_hosts=None):
        """
        Constructor.  Establishes mitogen router/broker.
        """
        
        logging.getLogger('mitogen').setLevel(logging.DEBUG) # (ERROR)

        self.policy = policy
        self.broker = mitogen.master.Broker()
        self.router = mitogen.master.Router(self.broker)
        self.hosts_by_context = dict()
        self.connections = dict()
        self.hosts = dict()
        self.context = dict()
        self.allow_patterns = policy.allow_fileserving_patterns()
        self.deny_patterns  = policy.deny_fileserving_patterns()
        self.checksums = dict()
        self._limit_groups = limit_groups
        self._limit_hosts = limit_hosts
github svinota / pyroute2 / pyroute2 / iproute / remote.py View on Github external
def __init__(self, *argv, **kwarg):

        self._argv = tuple(argv)
        self._kwarg = dict(kwarg)
        if 'router' in kwarg:
            self._mitogen_broker = None
            self._mitogen_router = kwarg.pop('router')
        else:
            self._mitogen_broker = mitogen.master.Broker()
            self._mitogen_router = mitogen.master.Router(self._mitogen_broker)

        netns = kwarg.get('netns', None)
        try:
            if 'context' in kwarg:
                context = kwarg['context']
            else:
                protocol = kwarg.pop('protocol', 'local')
                context = getattr(self._mitogen_router,
                                  protocol)(*argv, **kwarg)
            ch_in = mitogen.core.Receiver(self._mitogen_router,
                                          respondent=context)
            self._mitogen_call = context.call_async(MitogenServer,
                                                    ch_out=ch_in.to_sender(),
                                                    netns=netns)
            ch_out = ch_in.get().unpickle()
            super(RemoteIPRoute, self).__init__(Transport(Channel(ch_in)),
github dw / mitogen / ansible_mitogen / process.py View on Github external
def _setup_master(self):
        """
        Construct a Router, Broker, and mitogen.unix listener
        """
        self.broker = mitogen.master.Broker(install_watcher=False)
        self.router = mitogen.master.Router(
            broker=self.broker,
            max_message_size=MAX_MESSAGE_SIZE,
        )
        _setup_responder(self.router.responder)
        mitogen.core.listen(self.broker, 'shutdown', self._on_broker_shutdown)
        mitogen.core.listen(self.broker, 'exit', self._on_broker_exit)
        self.listener = mitogen.unix.Listener.build_stream(
            router=self.router,
            path=self.path,
            backlog=C.DEFAULT_FORKS,
        )
        self._enable_router_debug()
        self._enable_stack_dumps()
github dw / mitogen / examples / the_basics.py View on Github external
# important to pay attention to the use of UNIX fork in your code --
    # forking entails making a snapshot of the state of all locks in the
    # program, including those in the logging module, and thus can create code
    # that appears to work for a long time, before deadlocking randomly.
    # Forking in a Mitogen app requires significant upfront planning!
    broker = mitogen.master.Broker()

    # Construct a Router. This accepts messages (mitogen.core.Message) and
    # either dispatches locally addressed messages to local handlers (added via
    # Router.add_handle()) on the broker thread, or forwards the message
    # towards the target context.

    # The router also acts as an uglyish God object for creating new
    # connections. This was a design mistake, really those methods should be
    # directly imported from e.g. 'mitogen.ssh'.
    router = mitogen.master.Router(broker)

    # Router can act like a context manager. It simply ensures
    # Broker.shutdown() is called on exception / exit. That prevents the app
    # hanging due to a forgotten background thread. For throwaway scripts,
    # there are also decorator versions "@mitogen.main()" and
    # "@mitogen.utils.with_router" that do the same thing with less typing.
    with router:
        # Now let's construct a context. The '.local()' constructor just creates
        # the context as a subprocess, the simplest possible case.
        child = router.local()
        print("Created a context:", child)
        print()

        # This demonstrates the standard IO redirection. We call the print
        # function in the remote context, that should cause a log message to be
        # emitted. Any subprocesses started by the remote also get the same