Task Queues are great for deploying resource-intensive functions. 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:
Copy
Ask AI
from beta9 import task_queue, Image@task_queue( cpu=1.0, memory=128, gpu="T4", image=Image(python_packages=["torch"]), keep_warm_seconds=1000,)def multiply(**inputs): result = inputs["x"] * 2 return {"result": result}
You can interact with the task queue either through an API (when deployed), or directly in Python through the .put() method.
app.py
Copy
Ask AI
from beta9 import task_queue, Image@task_queue( cpu=1.0, memory=128, gpu="T4", image=Image(python_packages=["torch"]), keep_warm_seconds=1000,)def multiply(x): result = x * 2 return {"result": result}# Manually insert task into the queuemultiply.put(x=10)
If invoked directly from your local computer, the code above will produce this output:
Copy
Ask AI
$ python app.py=> Building image=> Using cached image=> Syncing files=> Files syncedEnqueued task: f0d205da-e74b-47ba-b7c3-8e1b9a3c0669
Beta9 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 beta9 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 beta9 serve, you’ll notice the server reloading with your code changes.You’ll use this workflow anytime you’re developing an app on Beta9. Trust us — it makes the development process uniquely fast and painless.