Running a Function Remotely

You can add a decorator to any Python function to run it remotely on Beam:

app.py
from beam import function


@function()
def handler():
    return {"hello world"}

if __name__ == "__main__":
    handler.remote()

By default, Beam will sync all the files in your working directory to the remote container. This allows you to use the files you have locally while developing. If you want to prevent some files from getting uploaded, you can create a .beamignore.

Passing Function Args

You can pass JSON arguments to your function, using the -d command in the CLI:

python app.py -d '{"x": "10"}'

Task Timeouts

You can set timeouts on tasks. Timeouts are set in seconds:

from beam import function

# Set a 24 hour timeout
@function(timeout=86400)
def handler():
    return {"hello world"}

# Disable timeouts completely
@function(timeout=-1)
def handler():
    return {"message": "hello world"}

if __name__ == "__main__":
    handler()

Running Tasks in the Background

By default, remote functions will stop when you close your local Python process or exit your shell.

You can override this behavior and keep the function running in the background by setting headless=False in your function decorator.

import time
from beam import function

# Run the function in the background
@function(headless=True)
def handler():
    for i in range(100):
        print(i)
        time.sleep(1)

    return {"message": "This is running in the background"}


if __name__ == "__main__":
    handler()