Async requests
Execute calls to a Cerebrium app to be run asynchronously
This is a new feature! As such, the API is still currently subject to changes.
However, should you encounter an issue please reach out to us on Discord!
Many applications require asynchronous execution of functions in a “fire-and-forget” fashion in order to work optimally. In this scenario, you hand off execution of a function to be Cerebrium’s responsibility, while you as the developer are responsible for ensuring that data is able leave the function.
You can enable your function to execute asynchronously by adding the async
query parameter to your request
, and setting it to true
. This would look something like this:
This would immediately return a response akin to the following, with a body that only specifies a run_id
:
Async functions will run for a maximum of 12 hours however will obey a maximum running time of based what is set in your cerebrium.toml file as the response_grace_period
.
This defaults to 15 minutes so max sure to update it to the maximum time your task needs.
In the background, Cerebrium will now run this HTTP request for you. Your function is still expected to behave in a synchronous manner. That is, it must execute a body of work and return a result once it is done. If you terminate your function early and return a response while the application is still doing work, the Cerebrium system will begin to terminate the application. Therefore, you should only return a response once the container has finished processing the task, and not earlier.
In order to use async execution effectively, you must ensure that your function exports any relevant data you
need, since you will no longer be able to receive a request. An easy way to achieve this would be to combine
async execution with a specified webhookEndpoint
, to have Cerebrium automatically forward the body of
the function response once it has returned:
As with webhooks, this a feature of our proxy, and you will not need to modify any part of your code in order to use the webhook
functionality. In your dashboard, your function will be marked as async, but will still show the status of the internal synchronous call
that was made (in other words, if the call to your function failed, the state of your async request would be failure
etc.)