Local Quick Start#

Open In Colab

In Cloud Quick Start, we demonstrate how to deploy a local function to a remote cluster using Runhouse. In this local-only version, we show how to use Runhouse to set up a local web server, and deploy an arbitrary Python function to it.

Runhouse Server Setup#

First install Runhouse with pip install runhouse

!pip install runhouse

Next, start the Runhouse server locally on CLI with runhouse restart, and use runhouse status to print the status and details of the server.

!runhouse restart
!runhouse status
😈 Runhouse Daemon is running πŸƒ
β€’ server_port: 32300
β€’ den_auth: False
β€’ server_connection_type: none
β€’ backend config:
        β€’ use_local_telemetry: False
        β€’ domain: None
        β€’ server_host: 0.0.0.0
        β€’ ips: ['0.0.0.0']
        β€’ resource_subtype: Cluster
Serving 🍦 :
base (Env):
This environment has no resources.


Local Python Function#

Let’s first define a simple Python function that we want to send to the server. This function returns the process ID it runs on, and optionally takes in a parameter, which it adds to the process ID prior to returning it.

def get_pid(a=0):
    import os
    return os.getpid() + int(a)

Deployment#

Standing up your Python code on the server is simple with the Runhouse API. Wrap the function with rh.function, and then use .to(rh.here) to sync it to the server.

import runhouse as rh
server_fn = rh.function(get_pid).to(rh.here)
INFO | 2024-02-26 22:14:53.460361 | Writing out function to /Users/caroline/Documents/runhouse/notebooks/docs/get_pid_fn.py. Please make sure the function does not rely on any local variables, including imports (which should be moved inside the function body).
INFO | 2024-02-26 22:14:53.523591 | Sending module get_pid to local Runhouse daemon

The get_pid function we defined above now exists on the server.

Remote Function Call#

You can call the server function just as you would any other Python function, with server_fn(), and it runs on the server and returns the result to our local environment.

Below, we run both the local and server versions of this function, which give different results and confirms that the functions are indeed being run on different processes.

print(f"Local PID {get_pid()}")
print(f"Server PID {server_fn()}")
Local PID 27818
Server PID 19846

HTTP Endpoint and Curl#

In addition to calling the function directly in Python, we can also access it with a curl call or open it up in a browser.

server_fn.endpoint()
'http://0.0.0.0:32300/get_pid'
!curl "http://0.0.0.0:32300/get_pid/call"
{"data":"19846","error":null,"traceback":null,"output_type":"result_serialized","serialization":"json"}

To pass in the optional function parameter:

!curl "http://0.0.0.0:32300/get_pid/call?a=1"
{"data":"19847","error":null,"traceback":null,"output_type":"result_serialized","serialization":"json"}