How to use the mitogen.master.Broker 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 / 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 dw / mitogen / ansible_mitogen / connection.py View on Github external
def _connect_broker(self):
        """
        Establish a reference to the Broker, Router and parent context used for
        connections.
        """
        if not self.broker:
            self.broker = mitogen.master.Broker()
            self.router, self.parent = mitogen.unix.connect(
                path=ansible_mitogen.process.MuxProcess.unix_listener_path,
                broker=self.broker,
            )
github dw / mitogen / examples / the_basics.py View on Github external
# user-friendly logging is possible. mitogen.log_to_file() just sets up
    # something basic, defaulting to INFO level, but you can override from the
    # command-line by passing MITOGEN_LOG_LEVEL=debug or MITOGEN_LOG_LEVEL=io.
    # IO logging is sometimes useful for hangs, but it is often creates more
    # confusion than it solves.
    mitogen.utils.log_to_file()

    # Construct the Broker thread. It manages an async IO loop listening for
    # reads from any active connection, or wakes from any non-Broker thread.
    # Because Mitogen uses a background worker thread, it is extremely
    # 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.
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()
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()