Running Async Tasks
Run functions using task queues
Task Queues are great for deploying resource-intensive functions on Beam. Instead of processing tasks immediately, the task queue enables you to add tasks to a queue and process them later, either sequentially or concurrently.
Task Queues include a built-in retry system. If a task fails for any reason, such as out-of-memory error or an application exception, your task will be retried three times before automatically moving to a failed state.
You can deploy any function to a task queue by using the task_queue
decorator:
Accessing a Queue Directly
You can interact with the task queue either through an API (when deployed), or directly in Python through the .put()
method.
If invoked directly from your local computer, the code above will produce this output:
Launch a Preview Environment
Beam includes a live-reloading feature that allows you to run your code on the same environment you’ll be running in production.
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
.
In your shell, run beam serve [FILE.PY]:[FUNCTION]
. This will:
- Spin up a container
- Run it on a remote server
- Print a cURL request to invoke the API
- Stream the logs to your shell
You should keep this terminal window open while developing.
Now, head back to your IDE, and change a line of code. Hit save.
If you look closely at the shell running beam serve
, you’ll notice the server reloading with your code changes.
You’ll use this workflow anytime you’re developing an app on Beam. Trust us — it makes the development process uniquely fast and painless.
Deploying Task Queues
To deploy the task queue, enter your shell and run this command from the working directory:
After running this command, you’ll see some logs in the console that show the progress of your deployment.
Calling the Endpoint
Because task queues run asynchronously, the API will return a Task ID.
Example Request
Example Response
Was this page helpful?