### Set up development environment and run tests Source: https://github.com/agentscope-ais/interp-rpc/blob/main/README.md Development setup and verification commands for the project. Creates a virtual environment, installs the package in editable mode with dev dependencies, runs the test suite, checks type hints with mypy in strict mode, and executes the benchmark script. ```sh python3 -m venv .venv && .venv/bin/pip install -e '.[dev]' .venv/bin/python -m pytest -q .venv/bin/python -m mypy src --strict python examples/bench.py ``` -------------------------------- ### Install interp-rpc with pip Source: https://github.com/agentscope-ais/interp-rpc/blob/main/README.md Install the interp-rpc package. Requires Python 3.14+ because it relies on concurrent.interpreters (PEP 734); the package fails to import on older versions with a RuntimeError. The package has zero runtime dependencies. ```sh pip install interp-rpc ``` -------------------------------- ### Use InterpreterPool with init and call/map Source: https://github.com/agentscope-ais/interp-rpc/blob/main/README.md Creates a pool of four subinterpreters, runs init once per worker before serving calls, then calls a target by dotted path and maps it over a list of inputs. The init= parameter is where imports belong because each subinterpreter re-imports and re-initializes every module it uses, so paying that cost once at pool startup is the point of a pool rather than a one-shot interp.create(). ```python from interp_rpc import InterpreterPool with InterpreterPool(workers=4, init="import mymodule") as pool: result = pool.call("mymodule.process", {"n": 7}) results = pool.map("mymodule.process", [{"n": i} for i in range(1000)]) ``` -------------------------------- ### Use InterpreterPool with workers and init Source: https://github.com/agentscope-ais/interp-rpc/blob/main/README.md Creates a pool of four subinterpreters, initializing each with `import mymodule`, then dispatches a single call and a map over items. Use when you need parallel CPU-bound work with structured request/response dispatch and real error propagation. ```python from interp_rpc import InterpreterPool with InterpreterPool(workers=4, init="import mymodule") as pool: result = pool.call("mymodule.process", data) results = pool.map("mymodule.process", items) ``` -------------------------------- ### Benchmark output: subinterpreters vs threads vs multiprocessing Source: https://github.com/agentscope-ais/interp-rpc/blob/main/README.md Benchmark output from running `python examples/bench.py` with 4 workers counting primes below 300,000 four times over (CPU-bound, no-I/O workload). Shows subinterpreters achieving 3.34x speedup vs sequential, ahead of multiprocessing's 2.79x in this run, with ~5x faster startup and ~50x faster small-payload latency than process pools. Numbers are from one run on Apple Silicon, macOS, CPython 3.14.7; run the benchmark yourself before relying on it. ```text N=300000, WORKERS=4 sequential (1 core, 4x work): 0.662s threads (4 threads, GIL-bound): 0.661s speedup vs seq: 1.00x interpreters (4 subinterpreters): 0.199s speedup vs seq: 3.34x multiprocessing (4 processes): 0.237s speedup vs seq: 2.79x startup: subinterpreter create+close: 7.58ms startup: process pool(1) spawn+call+teardown: 37.79ms small-payload latency: subinterpreter call(): 0.005ms/call small-payload latency: process pool.apply(): 0.251ms/call ``` -------------------------------- ### Call a target as a module-level callable Source: https://github.com/agentscope-ais/interp-rpc/blob/main/README.md Shows that a target can be a bare module-level callable instead of a dotted path string. The target must be resolvable by reference in the worker, so it cannot be a closure or lambda that captures state. ```python import mymodule pool.call(mymodule.process, {"n": 7}) # same as the string form ``` -------------------------------- ### Define a module-level target function in mymodule.py Source: https://github.com/agentscope-ais/interp-rpc/blob/main/README.md Defines a module-level function that the interpreter pool will call. The target must be resolvable by reference in the worker, so it must be defined at module level in a module the worker can import (typically via init=). ```python # mymodule.py def process(item: dict) -> dict: item["squared"] = item["n"] ** 2 return item ``` -------------------------------- ### Handle remote exceptions with RemoteError Source: https://github.com/agentscope-ais/interp-rpc/blob/main/README.md Shows how a remote exception is raised in the worker and how the caller catches it as RemoteError. The original class never crosses the interpreter boundary, so isinstance(err, ValidationError) is False; branch on err.remote_type_name (and err.remote_module if needed) instead. ```python # mymodule.py class ValidationError(ValueError): def __init__(self, field, message): super().__init__(message) self.field = field def save(record): raise ValidationError("email", "invalid field: email") ``` ```python from interp_rpc import RemoteError try: pool.call("mymodule.save", record) except RemoteError as err: err.remote_type_name # "ValidationError" err.remote_module # "mymodule" err.remote_message # "invalid field: email" err.remote_traceback # full formatted traceback text from the worker str(err) # both, stitched together, ready to log ``` -------------------------------- ### ShareabilityError for non-shareable arguments Source: https://github.com/agentscope-ais/interp-rpc/blob/main/README.md Demonstrates the ShareabilityError raised before a call is dispatched when an argument contains a non-shareable value. The error names the exact path to the offending value (e.g., 'item'['b']['handle']) and lists the shareable types. ```python >>> pool.call("mymodule.process", {"a": 1, "b": {"handle": open("f")}}) interp_rpc.ShareabilityError: argument 'item'['b']['handle'] is not shareable across the interpreter boundary: a value of type 'BufferedReader' was rejected (cannot pickle '_thread.lock' object). Shareable values: str, bytes, bytearray, int, float, bool, None, and any nesting of list/tuple/dict/set/frozenset built from those, plus instances of classes importable under the same name in the worker (define them at module level in a module your pool's init= imports -- not inside a function, and not a lambda). ``` -------------------------------- ### Call with timeout Source: https://github.com/agentscope-ais/interp-rpc/blob/main/README.md Dispatch a call with a 5-second timeout. Raises CallTimeoutError if the call hasn't finished, but only stops waiting — the call keeps running and the worker stays busy until it finishes on its own. ```python pool.call("mymodule.slow_thing", timeout=5.0) ``` -------------------------------- ### Raw NotShareableError from concurrent.interpreters Source: https://github.com/agentscope-ais/interp-rpc/blob/main/README.md Shows the raw error from the low-level `concurrent.interpreters` API when a non-shareable object (a lock) is passed. The error names the entire argument tuple, not the specific offending field, which is hard to debug on large payloads. ```python >>> import concurrent.interpreters as interp >>> it = interp.create() >>> it.call(process, {"a": 1, "b": {"handle": some_lock}}) NotShareableError: ({'a': 1, 'b': {'handle': }},) does not support cross-interpreter data ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.