Beta API. See the stability policy.

Async resources

Simulations, sweeps and flights over time take time to run, so the API doesn't make you wait. When you start one, you get an answer right away, and the run goes on in the background. This article shows how to find out when it's done, how to queue runs and start them later, and how to stop one.

Wait for a run to finish

You have two choices. You can check the run again and again, or you can have ThrustLab call your server when it's done. Checking is simpler. The Python library has wait(), which checks for you until the run ends:

Python
"""Test bench simulation: submit -> wait() -> read the result.

Run it:
    export THRUSTLAB_API_KEY=key_...        # never hard-code the key
    python examples/simulations/run_sync.py

The combination below is a BadAss 2826-820Kv motor, a 10.5x4.5 propeller and a
Liperior 4S 5000 mAh battery at 70% throttle, static. Swap in your own component
ids, or find them by name with client.components.find(...).
"""

from thrustlab import Client

client = Client()  # reads $THRUSTLAB_API_KEY from the environment

project = client.projects.create(name="sdk test bench example")

# Resolve components by name (find() returns the single match or raises
# AmbiguousComponentError / NotFoundError). Or paste explicit IDs instead:
#   motor_id = "comp_motor_xxx"
motor = client.components.find(name="BadAss 2826-820Kv")
prop = client.components.find(name="10.5x4.5")
battery = client.components.find(
    name="Liperior 5000mAh 4S 35C 14.8V Lipo Battery With XT90 Plug"
)

sim = client.simulations.create(
    project_id=project["id"],
    battery_component_id=battery["id"],
    airspeed_m_s=0.0,
    density_kg_m3=1.225,
    state_of_charge_pct=100,
    rotor_groups=[
        {
            "label": "main",
            # Four rotors need the Pro plan. On the Free plan, use "count": 1.
            "count": 4,
            "motor_component_id": motor["id"],
            "propeller_component_id": prop["id"],
            "throttle_pct": 70,
        }
    ],
)
print(f"created {sim['id']}, status={sim['status']}")

# wait() polls until completed / failed / canceled (or a timeout sentinel).
result = client.simulations.wait(sim["id"], timeout=300)
print(f"final status: {result['status']}")

if result["status"] == "completed":
    # One entry for each rotor under "1", "2", ...; the totals under "All".
    per_rotor = result["result"]["1"]
    aggregate = result["result"]["All"]
    print(f"per-rotor thrust: {per_rotor['thrust_n']:.2f} N")
    print(f"per-rotor rpm:    {per_rotor['rpm']:.0f}")
    print(f"total thrust:     {aggregate['total_thrust_n']:.2f} N")

A run is finished when its status is completed, failed or canceled. Note that canceled has one l. If wait() runs out of time before that, the library reports timed_out. That status never comes from the server.

Check every 2 to 5 seconds. If nothing changes, wait a little longer each time. A long flight over time can take several minutes.

To have ThrustLab call you instead, set up a webhook for simulation.completed, simulation.failed, sweep.completed, sweep.failed, flight.completed or flight.failed. See Webhooks and Event types.

When a run happened

A simulation and a sweep have four times:

When a run happened
FieldWhen it's set
created_atYou created the run.
dispatched_atThe run was handed to a worker.
started_atThe worker began solving.
completed_atThe run ended.

The time a run spent waiting is started_at minus dispatched_at. The time it spent solving is completed_at minus started_at.

A flight has created_at, dispatched_at and started_at, and no completed_at. Its run_meta.wall_s says how long it took to solve.

Queue runs and start them later

When you start a simulation or a sweep, launch_intent says what happens to it:

  • run is the default. The run reserves its compute units and starts right away.
  • queue reserves the compute units and keeps the run waiting. Its status stays queued and dispatched_at stays null.
  • draft saves the run and reserves nothing. It doesn't start.

To start the queued runs, use one of two requests:

  • POST /v1/simulations/run-queued starts every queued run in your account. It takes no body.
  • POST /v1/simulations/run-selected starts only the runs you name: {"simulation_ids": ["sim_...", "sweep_..."]}. You can name 1 to 1,000 runs, and sweeps are fine even though the field says simulation.

If you name a run that isn't queued, you get a 400 with the code invalid_status. If it has already started, you get already_dispatched. If it belongs to someone else, you get a 404.

Flights over time can't be queued. They start as soon as you create them.

Stop a run

How a run stops depends on where it is:

  • A queued run stops right away. It comes back canceled and you get all its compute units back.
  • A running sweep or a running flight over time takes a moment. The response still says running, with a cancel_requested_at time. Keep checking until the status is canceled.
  • A running single simulation can't be stopped. It finishes in seconds.

A flight over time stops at the end of its current step. That is at most 20 seconds of flight time. It keeps what it has solved so far, so the result is readable. Its run_meta.partial is true, and run_meta.canceled_at_t_s says how far the flight got. You get the compute units back and the run's place frees up.

To stop many runs together, send up to 1,000 IDs to POST /v1/simulations/cancel-selected. The reply sorts them into three lists. canceled are stopped. requested are running and will stop soon, so keep checking those. skipped were left alone, each with a reason.

How far a run has got

Every kind of run has a run_progress field while it runs. It has the same parts for a simulation, a sweep and a flight:

How far a run has got
PartWhat it is
doneHow much is done, in the unit named by unit.
plannedThe whole amount, or null while no end can be given.
unitsimulated_s for a simulation or a flight, points for a sweep.
estimatedtrue when planned is a guess or a cap.
sharedone divided by planned, from 0 to 1. It never goes down and stays below 1 until the run ends.

run_progress is null before the first part of the run ends, and on a run made before progress was kept.

A flight also has the older progress field while it runs, with these parts:

How far a run has got (table 2)
PartWhat it is
fractionHow much is done, from 0 to 1. It can be null if ThrustLab can't tell.
sim_time_sHow many seconds of the flight are done.
sim_time_total_sHow long the whole flight is, or null if it runs until the battery is empty.
wall_sHow many real seconds have passed.

For a flight that runs until the battery is empty, ThrustLab doesn't know the end in advance. Then fraction is an estimate and never reaches 1 until the run ends.

POST /v1/flights/estimate gives estimated_duration_s. That is the length of the flight itself. It does not tell you how long the run will take. A finished flight reports its real time in run_meta.wall_s.