Running Tasks
A short guide on using Beam to run one-off functions in the cloud
Running a function remotely
You can add a decorator to any Python function to run it remotely on Beam:
from beam import App, Runtime
app = App(name="my_app", runtime=Runtime())
@app.run()
def handler():
return
Parameter | Required | Description |
---|---|---|
handler | Yes | The handler function or entry point of the task to be executed. It should be a reference to the function that will process the task. |
runtime | Yes | The runtime environment for the task execution. Default is None. |
outputs | No | A list of of artifacts created during the task execution. Default is []. |
**kwargs | No | Additional keyword arguments to pass to the handler. |
Starting a run
Runs can be started with the beam run
CLI command, by passing in a the name of a Python file and a function name.
beam run app.py:handler
The function logs will stream to stdout. Feel free to close your terminal window. The task will continue to run asynchronously, and you can view the logs and any outputs in the web dashboard.
user@MacBook % beam run app.py:handler
i Using cached image.
✓ App initialized.
i Uploading files...
Uploading app.py 100% |██████████████████████| (1.7/1.7 kB, 13 kB/s)
✓ Container scheduled, logs will appear below.
Starting app...
Loading handler in 'app.py:handler'...
Running task: 68d8c9a2-794b-4ac9-8661-489ed4d1c710
Task complete: 68d8c9a2-794b-4ac9-8661-489ed4d1c710, duration: 0.0024688243865966797s
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:
beam run app.py:handler -d '{"text": "foobar"}'
The arguments can be passed into your function as either explicit positional arguments, or .
Using positional arguments
def handler(text: str):
# Prints foobar
print(text)
Using keyword arguments
def handler(**kwargs):
# Prints foobar
print(kwargs["text"])
Task timeouts
You can set timeouts on tasks. Timeouts are set in seconds:
# Set a 24 hour timeout
@app.rest_api(timeout=86400)
# Disable timeouts completely
@app.rest_api(timeout=-1)
The default timeout is 3600 seconds, or 1 hour. You can set a timeout of -1
to disable the timeout.
Further Reading
Was this page helpful?