Function

A Function is a portable code block that can be sent to remote hardware to run as a subroutine or service. It is comprised of the entrypoint, system (Cluster), and requirements necessary to run it.

Function Factory Methods

runhouse.function(fn: str | Callable | None = None, name: str | None = None, system: str | Cluster | None = None, env: str | List[str] | Env | None = None, dryrun: bool = False, load_secrets: bool = False, serialize_notebook_fn: bool = False)[source]

Builds an instance of Function.

Parameters:
  • fn (Optional[str or Callable]) – The function to execute on the remote system when the function is called.

  • name (Optional[str]) – Name of the Function to create or retrieve. This can be either from a local config or from Den. (Default: None)

  • load_from_den (bool, optional) – Whether to try loading the function from Den. (Default: True)

  • dryrun (bool, optional) – Whether to create the Function if it doesn’t exist, or load the Function object as a dryrun. (Default: False)

  • serialize_notebook_fn (bool, optional) – If function is of a notebook setting, whether or not to serialized the function. (Default: False)

Returns:

The resulting Function object.

Return type:

Function

Example

>>> import runhouse as rh
>>> cluster = rh.ondemand_cluster(name="my_cluster") >>> def sum(a, b): >>> return a + b
>>> summer = rh.function(fn=sum, name="my_func").to(cluster, env=['requirements.txt']).save()
>>> # using the function >>> res = summer(5, 8) # returns 13
>>> # Load function from above >>> reloaded_function = rh.function(name="my_func")

Function Class

class runhouse.Function(fn_pointers: Tuple | None = None, name: str | None = None, system: Cluster | None = None, dryrun: bool = False, **kwargs)[source]
__init__(fn_pointers: Tuple | None = None, name: str | None = None, system: Cluster | None = None, dryrun: bool = False, **kwargs)[source]

Runhouse Function object. It is comprised of the entrypoint, system/cluster, and dependencies necessary to run the service.

Note

To create a Function, please use the factory method function().

config(condensed=True)[source]

The config of the function.

Parameters:

condensed (bool, optional) – Whether to return the condensed config without expanding children subresources, or return the whole expanded config. (Default: True)

classmethod from_config(config: dict, dryrun: bool = False, _resolve_children: bool = True)[source]

Load or construct resource from config.

Parameters:
  • config (Dict) – Resource config.

  • dryrun (bool, optional) – Whether to construct resource or load as dryrun (Default: False)

get(run_key)[source]

Get the result of a Function call that was submitted as async using run.

Parameters:

run_key – A single or list of runhouse run_key strings returned by calling .call.remote() on the Function. The ObjectRefs must be from the cluster that this Function is running on.

Example

>>> remote_fn = rh.function(local_fn).to(gpu) >>> remote_fn_run = remote_fn.run() >>> remote_fn.get(remote_fn_run.name)
method_signature(method)[source]

Method signature, consisting of method properties to preserve when sending the method over the wire.

share(*args, visibility=None, **kwargs)[source]

Grant access to the resource for a list of users (or a single user). By default, the user will receive an email notification of access (if they have a Runhouse account) or instructions on creating an account to access the resource. If visibility is set to public, users will not be notified.

Note

You can only grant access to other users if you have write access to the resource.

Parameters:
  • users (Union[str, list], optional) – Single user or list of user emails and / or runhouse account usernames. If none are provided and visibility is set to public, resource will be made publicly available to all users. (Default: None)

  • access_level (ResourceAccess, optional) – Access level to provide for the resource. (Default: read).

  • visibility (ResourceVisibility, optional) – Type of visibility to provide for the shared resource. By default, the visibility is private. (Default: None)

  • notify_users (bool, optional) – Whether to send an email notification to users who have been given access. Note: This is relevant for resources which are not shareable. (Default: True)

  • headers (Dict, optional) – Request headers to provide for the request to Den. Contains the user’s auth token. Example: {"Authorization": f"Bearer {token}"}

Returns:

added_users:

Users who already have a Runhouse account and have been granted access to the resource.

new_users:

Users who do not have Runhouse accounts and received notifications via their emails.

valid_users:

Set of valid usernames and emails from users parameter.

Return type:

Tuple(Dict, Dict, Set)

Example

>>> # Write access to the resource for these specific users. >>> # Visibility will be set to private (users can search for and view resource in Den dashboard) >>> my_resource.share(users=["username1", "user2@gmail.com"], access_level='write')
>>> # Make resource public, with read access to the resource for all users >>> my_resource.share(visibility='public')
to(system: str | Cluster, process: str | Process | None = None, name: str | None = None)[source]

Send the function to the specified env on the cluster. This will sync over relevant code and packages onto the cluster, and set up the environment if it does not yet exist on the cluster.

Parameters:
  • system (str or Cluster) – The system to setup the function and env on.

  • process (str or Dict, optional) – The process to run the module on, if it’s a Dict, it will be explicitly created with those args. or the set of requirements necessary to run the module. (Default: None)

  • name (Optional[str], optional) – Name to give to the function resource, if you wish to rename it. (Default: None)

  • force_install (bool, optional) – Whether to re-install and perform the environment setup steps, even if it may already exist on the cluster. (Defualt: False)

Example

>>> rh.function(fn=local_fn).to(gpu_cluster) >>> rh.function(fn=local_fn).to(system=gpu_cluster, process=my_conda_env)