Prerequisites
Before using the Runpod JavaScript, ensure that you have:- Installed the Runpod JavaScript SDK.
- Configured your API key.
Set your Endpoint Id
Set your Runpod API key and your Endpoint Id as environment variables.endpoint
on the Endpoint
class. This allows you to use the following methods or instances variables from the Endpoint
class:
Run the Endpoint
Run the Endpoint with the either the asynchronousrun
or synchronous runSync
method.
Choosing between asynchronous and synchronous execution hinges on your task’s needs and application design.
Run synchronously
To execute an endpoint synchronously and wait for the result, use therunSync
method on your endpoint. This method blocks the execution until the endpoint run is complete or until it times out.
Run asynchronously
Asynchronous execution allows for non-blocking operations, enabling your code to perform other tasks while waiting for an operation to complete. For non-blocking operations, use therun
method on the endpoint. This method allows you to start an endpoint run and then check its status or wait for its completion at a later time.
Get results from an asynchronous run
The following example shows how to get the results of an asynchronous run.Poll the status of an asynchronous run
Usesawait endpoint.status(id)
to check the status of the operation repeatedly until it either completes or fails. After each check, the function waits for 5 seconds (or any other suitable duration you choose) before checking the status again, using the sleep function. This approach ensures your application remains responsive and doesn’t overwhelm the Runpod endpoint with status requests.
Stream
Stream allows you to stream the output of an Endpoint run. To enable streaming, your handler must support the"return_aggregate_stream": True
option on the start
method of your Handler. Once enabled, use the stream
method to receive data as it becomes available.
The maximum size for a payload that can be sent using yield to stream results is 1 MB.
Health check
Monitor the health of an endpoint by checking its status, including jobs completed, failed, in progress, in queue, and retried, as well as the status of workers.Status
Use thestatus
method and specify the id
of the run to get the status of a run.
Cancel
You can cancel a Job request by using thecancel()
function on the run request. You might want to cancel a Job because it’s stuck with a status of IN_QUEUE
or IN_PROGRESS
, or because you no longer need the result.
Timeout
To set a timeout on a run, pass a timeout value to therun
method. Time is measured in milliseconds.
Execution policy
You can set the maximum time to wait for a response from the endpoint in thepolicy
parameter.
Purge queue
You can purge all jobs from a queue by using thepurgeQueue()
function.
purgeQueue()
doesn’t affect Jobs in progress.