Running code remotely

This tutorial introduces several key concepts:

  • You can wrap any function in a function() decorator to run it on the cloud
  • You can invoke a function locally with .local()
  • You can invoke a function remotely with .remote()
  • You can fan out workloads to hundreds or thousands of containers with .map()
from beam import function


@function(cpu="100m", memory="100Mi")  # Each function runs on 100 millicores of CPU
def square(x):
    sum = 0

    for i in range(x):
        sum += i**2

    return {"sum": sum}


def main():
    print(square.local(x=10))
    print(square.remote(x=10))

    # Run a remote container for every item in list
    for i in square.map(range(5)):
        print(i)


if __name__ == "__main__":
    main()

In your shell, run this script by running this command. Make sure to replace app.py with the name of your file.

python [app.py]

When you run this command, your code is containerized and shipped onto a server in the cloud, and the logs are streamed back to your local shell.

$ python app.py

=> Building image
=> Using cached image
=> Syncing files
=> Files synced

=> Running function: <quickstart:square>
=> Running function: <quickstart:square>
=> Running function: <quickstart:square>
=> Running function: <quickstart:square>
=> Running function: <quickstart:square>

=> Function complete <916f76f4-c2a1-4994-a7fc-240c0fd2325a>
=> Function complete <4b5455fe-4d26-4941-9fe6-31361a832b1f>
=> Function complete <2a08ff50-b88b-48dd-b09a-0c3264ad8f24>
=> Function complete <79f7c64c-0e5c-4254-b71d-c7b07b8e5848>
=> Function complete <941d9dbf-9455-4abb-bdee-e2f718d4a694>

The .map() method in main() spawns 5 remote containers, but that number is arbitrary.

You can change 5 to 20, and invoke this function again — you’ll see 20 containers spin up almost instantly.