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 202 indicates that the job is pending. The status shows job is still running.
HTTP/2 202 
date: Wed, 12 Mar 2025 23:09:20 GMT
content-type: application/json
content-length: 93
strict-transport-security: max-age=31536000; includeSubDomains
x-content-type-options: nosniff
cache-control: no-cache, no-store, must-revalidate
pragma: no-cache
x-frame-options: SAMEORIGIN
x-xss-protection: 1;mode=block
content-security-policy: default-src 'none'; connect-src 'self'; font-src 'self' static.oracle.com; img-src 'self' data: static.oracle.com; media-src 'none'; object-src 'none'; script-src 'self' static.oracle.com 'unsafe-inline' 'unsafe-eval'; style-src 'self' static.oracle.com 'unsafe-inline'; frame-ancestors 'none'
 
{"createdDate":" Mar 12,2025 22:55","status":"job is still running","elapsedTime":"00:13:32"}

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}},...}}}

To delete a running job, pass the job id to cURL using the DELETE method.

Note:

Attempting to delete a job id again will result in the message: "Job <job id> status is FAILURE and not applicable to delete".
curl -i -k -X DELETE --header "Authorization: Bearer ${token}" --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{"service":"LOW"}'  "<oml-cloud-service-location-url>/oml/api/py-scripts/v1/jobs/<job id>"

The HTTP response status 200 indicates that the request was executed successfully.

HTTP/1.1 200 OK
Date: Mon, 10 Feb 2025 22:03:43 GMT
Content-Type: application/json
Content-Length: 112
Connection: keep-alive
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1;mode=block
Content-Security-Policy: default-src 'none'; connect-src 'self'; font-src 'self' static.oracle.com; img-src 'self' data: static.oracle.com; media-src 'none'; object-src 'none'; script-src 'self' static.oracle.com 'unsafe-inline' 'unsafe-eval'; style-src 'self' static.oracle.com 'unsafe-inline'; frame-ancestors 'none'
 
{"Message":"Job <job id> sucessfully deleted container ending :  LOW-1739224926687"}

Adjust the timeout Parameter for Longer Runs in Asynchronous Mode

The default mode for REST API for Embedded Python Execution function endpoint calls is synchronous and scripts will automatically time out after 60 seconds. For longer-running scripts, use asynchronous mode. In asynchronous mode, the default timeout is 1800 seconds. To adjust the timeout, set the timeout parameter (in seconds) and enable async_flag=true. The following example uses the asynchronous table-apply invocation and sets the timeout parameter to 7200 seconds.

curl -i -X POST --header "Authorization: Bearer <token>" \
  --header 'Content-Type: application/json' \
  --header 'Accept: application/json' \
  -d '{"input":"select * from tablename", "parameters":"{\"oml_input_type\":\"pandas.DataFrame\"}", "asyncFlag":true, "timeout":7200}' \
       "<OML URL>/api/py-scripts/v1/table-apply/scriptname"