Mitogen

0.3.46 · active · verified Fri Apr 17

Mitogen is a library for writing distributed self-replicating programs, designed to be fast, efficient, and flexible. It enables code execution on remote hosts with minimal overhead, making it ideal for automation and orchestration tasks. The current version is 0.3.46, and it sees regular minor releases, typically every few weeks or months, indicating active development.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to connect to a remote host (defaulting to localhost via SSH) and execute a simple Python function, returning its result. Ensure SSH is configured for the target host or choose a different transport (e.g., `router.fork()`).

import mitogen.master
import os
import sys
import logging

logging.basicConfig(level=logging.INFO)

# Connect to a remote host via SSH. For local testing, set MITOGEN_TARGET_HOSTNAME='localhost'
# or configure your SSH client to allow localhost connections without password.
router = mitogen.master.Router()

try:
    hostname = os.environ.get('MITOGEN_TARGET_HOSTNAME', 'localhost')
    if hostname == 'localhost' and not os.path.exists(os.path.expanduser('~/.ssh/id_rsa')):
        logging.warning("No SSH key found for 'localhost'. Ensure SSH access is configured or use a different transport.")

    context = router.ssh(hostname=hostname)

    # Run an operation on the remote host.
    res = context.call(lambda: 'Hello from Python %s on %s' % (sys.version, os.uname()[1]))

    # Print its return value.
    print(f"Remote execution result: {res}")

finally:
    router.shutdown()
    router.join()

view raw JSON →