Local Quick Start#
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
[1;38;5;63mπ Runhouse Daemon is running π[0m
β’ server_port: [1;36m32300[0m
β’ den_auth: [3;91mFalse[0m
β’ server_connection_type: none
β’ backend config:
β’ use_local_telemetry: [3;91mFalse[0m
β’ domain: [3;35mNone[0m
β’ server_host: [1;92m0.0.0.0[0m
β’ ips: [1m[[0m[32m'0.0.0.0'[0m[1m][0m
β’ resource_subtype: Cluster
[1mServing π¦ :[0m
[3;4mbase [0m[1;3;4m([0m[3;4mEnv[0m[1;3;4m)[0m[3;4m:[0m
This environment has no resources.
[0m
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"}