To handle this properly, implementing a loop that periodically polls the job status until it changes to "completed" or "failed". updated version of the polling logic:
import time
# Poll job status until it's no longer "pending"
while True:
status_response = requests.get(f"https://api.example.com/jobs/{job_id}/status", headers=headers).json()
status = status_response["status"]
print("Job status:", status)
if status in ["completed", "failed"]:
break
time.sleep(5) # Wait for 5 seconds before polling again
This way, the script keeps checking the job status at regular intervals and exits the loop once the job is done.