{"id":9948,"library":"mitogen","title":"Mitogen","description":"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.","status":"active","version":"0.3.46","language":"en","source_language":"en","source_url":"https://github.com/mitogen-hq/mitogen/","tags":["distributed","remote execution","ansible","automation","rpc","python2","python3"],"install":[{"cmd":"pip install mitogen","lang":"bash","label":"Install Mitogen"}],"dependencies":[],"imports":[{"symbol":"Router","correct":"from mitogen.master import Router"},{"note":"Importer class and its whitelist/blacklist methods were removed in v0.3.42. Use ImportPolicy instead.","wrong":"from mitogen.master import Importer","symbol":"ImportPolicy","correct":"from mitogen.master import ImportPolicy"}],"quickstart":{"code":"import mitogen.master\nimport os\nimport sys\nimport logging\n\nlogging.basicConfig(level=logging.INFO)\n\n# Connect to a remote host via SSH. For local testing, set MITOGEN_TARGET_HOSTNAME='localhost'\n# or configure your SSH client to allow localhost connections without password.\nrouter = mitogen.master.Router()\n\ntry:\n    hostname = os.environ.get('MITOGEN_TARGET_HOSTNAME', 'localhost')\n    if hostname == 'localhost' and not os.path.exists(os.path.expanduser('~/.ssh/id_rsa')):\n        logging.warning(\"No SSH key found for 'localhost'. Ensure SSH access is configured or use a different transport.\")\n\n    context = router.ssh(hostname=hostname)\n\n    # Run an operation on the remote host.\n    res = context.call(lambda: 'Hello from Python %s on %s' % (sys.version, os.uname()[1]))\n\n    # Print its return value.\n    print(f\"Remote execution result: {res}\")\n\nfinally:\n    router.shutdown()\n    router.join()","lang":"python","description":"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()`)."},"warnings":[{"fix":"Migrate to `mitogen.master.ImportPolicy`. Construct an `ImportPolicy` object with `overrides` or `blocks` and pass it to the `Router` constructor via the `import_policy` argument. E.g., `policy = ImportPolicy(overrides={'mypackage': True}); router = Router(import_policy=policy)`.","message":"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.","severity":"breaking","affected_versions":">=0.3.42"},{"fix":"Review the Mitogen logging documentation and explicitly configure your `logging` module settings (e.g., `logging.basicConfig`) to ensure desired behavior. For Ansible integration, note that Mitogen debugging is hidden by default at high Ansible verbosity levels.","message":"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.","severity":"gotcha","affected_versions":">=0.3.45"},{"fix":"If you encounter pickling errors for custom types, ensure they are explicitly allowed or refactor your code to avoid relying on implicit `GLOBAL` opcodes for insecure types. Consider using `mitogen.master.ImportPolicy` to manage allowed types for unpickling if custom unpickling is necessary and safe.","message":"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.","severity":"gotcha","affected_versions":">=0.3.41"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"The `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)`.","cause":"Attempting to access or set the deprecated `importer_whitelist` (or `importer_blacklist`) attribute on a `Router` instance.","error":"AttributeError: 'Router' object has no attribute 'importer_whitelist'"},{"fix":"Configure 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.","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.","error":"mitogen.parent.ModuleDeniedByOverridesError: Refused to import module 'your_module_name'"}]}