What Are 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.Creating a Task Queue
You can run any function as a task queue by using thetask_queue decorator:
result.txt file when the task completes.
Sending Async Requests
Because task queues run asynchronously, the API will return a Task ID. Example RequestRequest
Response
Viewing Task Responses
Becausetask_queue is async, you will need to make a separate API call to retrieve the task output.
Saving and Returning Output Files
You can save files using Beam’s Output class. The code below saves a file, wraps it in anOutput, and generates a URL that can be retrieved later:
app.py
Retrieving Results
There are two ways to retrieve response payloads:- Beam makes a webhook request to your server, based on the
callback_urlin your endpoint - Saving an
Outputand calling the/taskAPI
Webhooks
If you’ve added acallback_url to your decorator, Beam will fire a webhook to your server with the task response when it completes:
Polling for Results
Output payloads can be retrieved by polling the task API:
outputs list in the response:
Retry Behavior
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.Programmatically Enqueuing Tasks
You can interact with the task queue either through an API (when deployed), or directly in Python through the.put() method.
app.py