A small experiment for running Python functions on a remote machine with a single decorator. A function decorated with @run("server_ip") is serialized (with its closure/arguments) using cloudpickle, sent over HTTP to a Flask server, executed there, and the result is sent back to the caller, as if the function had run locally.
-
client.py, defines therun(server_ip)decorator. It wraps the target function so that calling it:- Serializes the function and its positional arguments with
cloudpickle, encoding each as base64. - POSTs them as JSON to
http://<server_ip>:5779/execute. - Deserializes and returns the
resultfrom the JSON response, or raises anExceptionif the server reports an error.
- Serializes the function and its positional arguments with
-
server.py, a minimal Flask app exposing a single endpoint:POST /execute, decodes the base64/cloudpickle-encoded function and arguments from the request body, calls the function with the given arguments, and returns the result as JSON (or a 400 with an error message on failure). Runs on port5779.
-
main.py, example usage: definesPerson/Walletclasses and two functions (hello,random_guess) decorated with@run("localhost"), showing that both simple return values and non-trivial objects (passed as arguments) can be sent to and computed on the remote server.
Declared in the Pipfile (Python 3.12):
flaskcloudpicklerequests
-
Install dependencies (using
pipenv, matching the providedPipfile/Pipfile.lock):pipenv install
-
Start the server (listens on port
5779):python server.py
-
In a separate process/machine, run a script that uses the
@run(server_ip)decorator, e.g.:python main.py
Point
@run(...)at the server's IP (or"localhost"if running both on the same machine) to have the decorated function execute on the server instead of locally.
This is a small proof-of-concept rather than a packaged library, there are no tests, no setup.py/pyproject.toml, and the server executes arbitrary deserialized code with no authentication, so it should be treated as a local experiment, not something to expose on an untrusted network.