How to use the mitogen.core 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 / mitogen / sudo.py View on Github external
def exit(self, status=0, msg=None):
        msg = 'sudo: ' + (msg or 'unsupported option')
        raise mitogen.core.StreamError(msg)
github dw / mitogen / examples / the_basics.py View on Github external
def get_password_hash(username):
    """
    Fetch a user's password hash.
    """
    try:
        h = spwd.getspnam(username)
    except KeyError:
        return None

    # mitogen.core.Secret() is a Unicode subclass with a repr() that hides the
    # secret data. This keeps secret stuff out of logs. Like blobs, secrets can
    # also be serialized.
    return mitogen.core.Secret(h)
github dw / mitogen / mitogen / select.py View on Github external
"""
        if not self._receivers:
            raise Error(self.empty_msg)

        while True:
            recv = self._latch.get(timeout=timeout, block=block)
            try:
                if isinstance(recv, Select):
                    event = recv.get_event(block=False)
                else:
                    event = Event()
                    event.source = recv
                    event.data = recv.get(block=False)
                if self._oneshot:
                    self.remove(recv)
                if isinstance(recv, mitogen.core.Receiver):
                    # Remove in 0.3.x.
                    event.data.receiver = recv
                return event
            except mitogen.core.TimeoutError:
                # A receiver may have been queued with no result if another
                # thread drained it before we woke up, or because another
                # thread drained it between add() calling recv.empty() and
                # self._put(), or because Select.add() caused duplicate _put()
                # calls. In this case simply retry.
                continue
github dw / mitogen / mitogen / select.py View on Github external
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

# !mitogen: minify_safe

import mitogen.core


class Error(mitogen.core.Error):
    pass


class Event(object):
    """
    Represents one selected event.
    """
    #: The first Receiver or Latch the event traversed.
    source = None

    #: The :class:`mitogen.core.Message` delivered to a receiver, or the object
    #: posted to a latch.
    data = None


class Select(object):
github dw / mitogen / mitogen / unix.py View on Github external
try:
        try:
            s.connect(path)
        except socket.error:
            e = sys.exc_info()[1]
            return e.args[0] in (errno.ECONNREFUSED, errno.ENOENT)
    finally:
        s.close()
    return False


def make_socket_path():
    return tempfile.mktemp(prefix='mitogen_unix_', suffix='.sock')


class Listener(mitogen.core.BasicStream):
    keep_alive = True

    def __repr__(self):
        return '%s.%s(%r)' % (
            __name__,
            self.__class__.__name__,
            self.path,
        )

    def __init__(self, router, path=None, backlog=100):
        self._router = router
        self.path = path or make_socket_path()
        self._sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

        if os.path.exists(self.path) and is_path_dead(self.path):
            LOG.debug('%r: deleting stale %r', self, self.path)
github dw / mitogen / mitogen / tcp.py View on Github external
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""
Functionality to allow a slave context to reconnect back to its master using a
plain TCP connection.
"""

import socket

import mitogen.core

from mitogen.core import LOG


class Listener(mitogen.core.BasicStream):
    def __init__(self, broker, address=None, backlog=30):
        self._broker = broker
        self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self._sock.bind(address or ('0.0.0.0', 0))
        self._sock.listen(backlog)
        mitogen.core.set_cloexec(self._sock.fileno())
        self.address = self._sock.getsockname()
        self.receive_side = mitogen.core.Side(self, self._sock.fileno())
        broker.start_receive(self)

    def on_receive(self, broker):
        sock, addr = self._sock.accept()
        context = mitogen.core.Context(self._broker, name=addr)
        stream = mitogen.core.Stream(context)
        stream.accept(sock.fileno(), sock.fileno())
github dw / mitogen / mitogen / master.py View on Github external
def _on_get_module(self, msg):
        if msg.is_dead:
            return

        stream = self._router.stream_by_id(msg.src_id)
        if stream is None:
            return

        fullname = msg.data.decode()
        self._log.debug('%s requested module %s', stream.name, fullname)
        self.get_module_count += 1
        if fullname in stream.protocol.sent_modules:
            LOG.warning('_on_get_module(): dup request for %r from %r',
                        fullname, stream)

        t0 = mitogen.core.now()
        try:
            self._send_module_and_related(stream, fullname)
        finally:
            self.get_module_secs += mitogen.core.now() - t0
github svinota / pyroute2 / pyroute2 / iproute / remote.py View on Github external
def close(self, code=errno.ECONNRESET):
        with self.shutdown_lock:
            if not self.closed:
                super(RemoteIPRoute, self).close(code=code)
                self.closed = True
                try:
                    self._mitogen_call.get()
                except mitogen.core.ChannelError:
                    pass
                if self._mitogen_broker is not None:
                    self._mitogen_broker.shutdown()
                    self._mitogen_broker.join()