Async resources

Simulations, sweeps, flights and component analyses take time to finish. When you start one, the library gets an answer straight away, and the work goes on in the background. This page shows how to wait for the result.

The life of a run

create returns a run in the queued state. Then it moves along:

The life of a run
StatusMeaning
queuedWaiting for a free place to run.
runningA worker is calculating it.
completedDone. The result is ready.
failedSomething went wrong. The error field says what.
canceledSomeone stopped it.

The last three are final. The spelling is canceled with one l. A run you only saved to start later can also be a draft.

Wait for the result

wait checks the run until it is final, then returns it:

python
# settings holds the arguments from the page Simulations
sim = client.simulations.create(**settings)
done = client.simulations.wait(sim["id"], timeout=300)

if done["status"] == "completed":
    print(done["result"]["All"]["total_thrust_n"])
elif done["status"] == "timed_out":
    print("Still going. Try waiting again.")
else:
    print(done["status"], done["error"])

Simulations, sweeps and flights all have a wait with the same two settings:

  • timeout is how many seconds to wait. The default is 600.
  • poll_interval is the seconds between checks. The default is 2.

wait never raises an error because a run failed. It returns the run, and you read status.

When the wait runs out

If the time runs out first, wait returns the run with the status timed_out. The library makes up this status. The server never sends it. The run itself keeps going, so you can call wait again, or retrieve to look once.

python
done = client.simulations.retrieve(sim["id"])

How often it checks

After each check, the wait before the next one doubles, up to 30 seconds. With the defaults the gaps are 2, 4, 8, 16 and then 30 seconds. A long run therefore costs few requests.

Each check is one request, and it counts toward your request limit. For a very long run, set a larger poll_interval, or use webhooks and let ThrustLab tell you.

See how far a run is

While a run goes on, run_progress on the run shows done and planned, in a unit named by unit, and a share from 0 to 1:

python
run = client.sweeps.retrieve(sweep["id"])
print(run["run_progress"])
print(run["total_points"], run["completed_points"])

run_progress is None for a run that has no progress yet. A sweep also reports total_points and completed_points.

Component analyses

client.component_analyses.wait is a little different. It checks at a steady rate, 1 second by default, and gives up after 300 seconds. It also takes on_progress, a function the library calls with the job after every check. A job ends as completed, failed or cancelled, and here cancelled has two l's. See Component analyses.

Stop a run

Use cancel on a simulation, sweep or flight to stop it. A run that is still queued stops at once. A sweep or a flight that is running stops soon after. A single simulation that is already running can't be stopped, and cancel raises a ConflictError.