Asynchronous Script Execution

The default mode for REST API for Embedded Python Execution function endpoint calls is synchronous, so the REST API call must return before you can execute any additional calls. Running a call in asynchronous mode allows you to invoke function endpoints without needing to wait for a previous call to return.

Follow these steps to use the asynchronous mode of script execution.

  1. Call a script execution function with the asyncFlag set to true. The return is a location that includes a job id that you can use to poll the status of the job.
  2. Get the status of the job. In the return, a 202 status code indicates that the job is pending. A 302 code indicates that the job has finished. When the job has finished, the return includes a Content-Location header.
  3. Fetch the results of the script.

The following is an example of making an asynchronous group-apply call and fetching the results. In the example, the parallelFlag argument is set to true to make use of database parallelism.

curl -i -X POST --header "Authorization: Bearer ${token}" \
--header 'Content-Type: application/json' --header 'Accept: application/json' \
-d '{"asyncFlag":true, "input":"select * from IRIS", "parameter":"{\"oml_input_type\":\"pandas.DataFrame\"}", "groupBy":"Species", "orderBy":"Sepal_Length", "parallelFlag":true}' \
"<oml-cloud-service-location-url>/oml/api/py-scripts/v1/group-apply/group_count"  

The output includes the HTTP 201 status code indicating that the job was created and a job id that you use to get the job status and the results.

In the following result, the job has been created.

HTTP/1.1 201 Created
Date: Thu, 27 Aug 2020 15:14:04 GMT
Content-Length: 0
Connection: keep-alive
Cache-Control: no-cache, no-store, private
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1;mode=block
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff
Set-Cookie: JSESSIONID=node01k14ak2rj75ug1gfbbze9gysl7710.node0; Path=/oml; Secure; HttpOnly
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Location: https://<oml-cloud-service-location-url>/oml/api/py-scripts/v1/jobs/<job id>;

To poll the job status, pass the job id to cURL using the GET method.

curl -i -X GET --header "Authorization: Bearer ${token}" \
--header 'Accept: application/json' \
"<oml-cloud-service-location-url>/oml/api/py-scripts/v1/jobs/<job id>"

The HTTP response status 302 indicates that the job has finished. The Content-Location has the location of the result.

HTTP/1.1 302 Found
Date: Thu, 27 Aug 2020 15:14:26 GMT
Content-Length: 0
Connection: keep-alive
Cache-Control: no-cache, no-store, private
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1;mode=block
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff
Content-Location: https://<oml-cloud-service-location-url>/oml/api/py-scripts/v1/jobs/<job id>/result

Get the result.

curl -i -X GET --header "Authorization: Bearer ${token}" \
--header 'Accept: application/json' \
"<oml-cloud-service-location-url>/oml/api/py-scripts/v1/jobs/<job id>/result"
HTTP/1.1 200 OK
Date: Thu, 27 Aug 2020 15:16:32 GMT
Content-Type: application/json
Content-Length: 5045
Connection: keep-alive
Cache-Control: no-cache, no-store, private
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1;mode=block
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff

The following is a portion of the result.

{"result":{"versicolor_8":{"SEPAL_LENGTH":{"0":5.7},"SPECIES":{"0":"versicolor"},"COUNT":{"0":5}},
"versicolor_7":{"SEPAL_LENGTH":{"0":5.6},"SPECIES":{"0":"versicolor"},"COUNT":{"0":2}},
"virginica_20":{"SEPAL_LENGTH":{"0":6.7},"SPECIES":{"0":"virginica"},"COUNT":{"0":1}},...}}}