Mitogen
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
-
AttributeError: 'Router' object has no attribute 'importer_whitelist'
cause Attempting to access or set the deprecated `importer_whitelist` (or `importer_blacklist`) attribute on a `Router` instance.fixThe `importer_whitelist` attribute was removed in Mitogen v0.3.42. Use `mitogen.master.ImportPolicy` instead, which is passed as an argument to the `Router` constructor. Example: `policy = mitogen.master.ImportPolicy(overrides={'mypackage': True}); router = mitogen.master.Router(import_policy=policy)`. -
mitogen.parent.ModuleDeniedByOverridesError: Refused to import module 'your_module_name'
cause A module required by remote code is not explicitly allowed by the `ImportPolicy` configured on the Mitogen router, often after upgrading to v0.3.42 or later.fixConfigure your `mitogen.master.ImportPolicy` to explicitly allow the module. For example, if 'your_module_name' is denied: `policy = mitogen.master.ImportPolicy(overrides={'your_module_name': True}); router = mitogen.master.Router(import_policy=policy)`. Remember to restart the router for changes to take effect.
Warnings
- breaking The `Importer` class and its `whitelist`/`blacklist` methods for controlling remote imports were removed in v00.3.42. Direct manipulation of `Router.importer_whitelist` or `Router.importer_blacklist` is no longer supported.
- gotcha Mitogen's internal logging configuration was overhauled in v0.3.45. If your application relied on specific default logging behaviors or internal Mitogen loggers, you might observe changes in log output or levels.
- gotcha Improved security in v0.3.41 introduced a default deny policy for `Unpickler GLOBAL` opcodes, restricting what types can be unpickled over the network. This prevents deserialization of potentially arbitrary code.
Install
-
pip install mitogen
Imports
- Router
from mitogen.master import Router
- ImportPolicy
from mitogen.master import Importer
from mitogen.master import ImportPolicy
Quickstart
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()