Skip to content

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

remote-functions

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.

How it works

  • client.py, defines the run(server_ip) decorator. It wraps the target function so that calling it:

    1. Serializes the function and its positional arguments with cloudpickle, encoding each as base64.
    2. POSTs them as JSON to http://<server_ip>:5779/execute.
    3. Deserializes and returns the result from the JSON response, or raises an Exception if the server reports an error.
  • 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 port 5779.
  • main.py, example usage: defines Person/Wallet classes 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.

Requirements

Declared in the Pipfile (Python 3.12):

  • flask
  • cloudpickle
  • requests

Usage

  1. Install dependencies (using pipenv, matching the provided Pipfile/Pipfile.lock):

    pipenv install
  2. Start the server (listens on port 5779):

    python server.py
  3. 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.

Status

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.

About

A Python decorator + Flask server that lets you run a local function's code remotely by serializing it and its arguments with cloudpickle

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages