SDKPython SDK

Runtime modes

Continuous running in the current thread, a background thread, or manual update.

Current version · 2.0.0

Runtime modes

This chapter applies only to programs that continuously compute SDK-processed data. Raw pose/IMU does not require a run mode; see Data channels & read methods. Automatic reconnection advances independently and does not require continuous update() calls or a runner.

When the above features are needed, choose one run mode through Glove. Do not mix them in the same run:

  • Command-line programs usually call glove.run_forever() to run continuously in the current thread;
  • When a GUI, an asynchronous program, or the current thread also needs to handle other work, you can call glove.run_background() to run in the background;
  • When a synchronous main loop already exists, you can continuously call glove.update() and let the program control scheduling.

Continuous running in the current thread

glove.run_forever()

run_forever() blocks the current thread until a stop request is received or the SDK stops due to an error. Public command-line examples end the process with Ctrl+C; when you need to stop actively, use background mode.

Running in a background thread

After start() succeeds, call glove.run_background() to start the background run.

join() not only waits for the thread to end, it is also responsible for reporting background running errors to the caller, so you must not call only request_stop() and omit join(). Background mode is suitable for the GUI main thread, a host event loop, or other scenarios where the current thread cannot be blocked.

After start() and glove.run_background() succeed, an asyncio program must observe consumption and the background result concurrently: create one task for asyncio.to_thread(glove.join) and another for await output.next() or async for, then observe the first completion with asyncio.wait(..., return_when=asyncio.FIRST_COMPLETED). Starting join only after consumption ends can leave the consumer waiting forever when the background run fails without another output. The asyncio event loop does not advance SDK scheduling; both the SDK background run and the event loop must keep running.

If the background run ends early, cancel pending consumption and read the original join result; do not replace its failure with a cancellation error. On successful consumption, processing failure, or caller cancellation, request stop if still running, await the same join task, and then close Glove and Sdk. Retain the join task during cleanup: cancelling asyncio.to_thread() does not stop its underlying wait. Because close() can wait for processing and callbacks, call it through asyncio.to_thread(). The updated async_runtime.py also handles repeated cancellation during cleanup. See Migration for version applicability and upgrade actions.

If external frames come from an async iterator, you should create a separate asyncio task that continuously calls push_frame(), while reading output through await output.next() or async for. Only waiting for output without continuing to submit input frames will not produce new SDK-processed data.

If the data-reading task has already finished, inspect its result too; do not discard its completed exception as a cancellation result.

Complete sources and build files: Download the async runtime examples. Follow the included README to configure the SDK, port, side and algorithm package.

Manual update

Manual mode is suitable for programs that already have a synchronous main loop and want to actively control SDK scheduling. First subscribe to the SDK-processed data, then start the device; each call to glove.update() computes the tasks that are due, and you can also read the current output immediately through latest():

import time

from qnbot_sdk import SleepResult

output = glove.device().output()
output.subscribe(on_output)
glove.start()

for _ in range(100):
    update = glove.update()
    latest = output.latest()
    if latest is not None:
        print(latest.sequence, latest.value)
    sleep_result = update.sleep()
    if sleep_result is SleepResult.NO_TASKS:
        time.sleep(0.01)  # Avoid busy polling while idle or waiting for app input.
    elif sleep_result is SleepResult.STOPPED:
        break

Choose a field by what you need:

What you want to doRead field
Record how many scheduling rounds have runtick
See how many tasks ran this timeran_task_count
Inspect whether a periodic deadline existshas_next_task; observation only, not a gate for sleep()
Wait until the next task is duesleep()

For complete fields, types and return values, see UpdateResult. sleep() returns SleepResult.READY, STOPPED or NO_TASKS. Call sleep() once after every update(); do not gate it on has_next_task, because event-driven work is not necessarily reflected by that field. When the SDK is fully idle or the application must supply the next frame, NO_TASKS means there is currently nothing for the SDK to wait on, so yield briefly before the next iteration. Each UpdateResult can only be waited on once; reusing it or using it after the next update throws StaleUpdateError.

Manual mode only changes who advances SDK scheduling, not how data is read. A host that can coordinate SDK scheduling and the asyncio event loop by itself can also use manual update(), but must ensure that glove.update() is called continuously; only waiting for output without continuing to advance scheduling will not produce new SDK-processed data. Ordinary asynchronous programs should prefer glove.run_background(); for the asynchronous read method, see Asynchronous reading.

For a complete asynchronous flow, see async_runtime.py.

Closing the owning SDK or domain from other application logic ends the related data waits and coordinates with a pending background join. The application must still await its existing join observer and inspect the result. For ordinary shutdown, keep the stop, same join, then close order.

Temporary silence or connection recovery does not mean the background run has ended. Channel waits do not end merely because data is absent. Use the join result to observe background completion; a data-wait timeout is not a background completion result. Explicit stop()/start() follows the existing lifecycle: after stopping, wait for the previous background join to finish before restarting.

Calibration and frame input

These rules include automatic terminal calibration for external input.

Run mode and calibration policy are independent. Manual update() means the application advances the loop; the default Auto policy still lets the SDK organize capture and calibration. With auto_terminal, terminal guidance remains active across updates. The Manual calibration policy instead makes the application create capture and calibration explicitly; see the migration notes.

These startup rules apply to the Auto policy with auto_terminal when targets still need capture or calibration:

Run mode and inputReturn and advancement
Background, all pending inputs supplied by devicesReturns after the selected targets finish preparation
Background, at least one pending external inputrun_background() starts background processing and returns; the application continues push_frame() while terminal guidance remains active
Background, mixed device and pending external inputAlso returns first; keep supplying external frames
Manual loopCall push_frame() first for external input, then update() and sleep(); guidance continues across updates
Foreground run_forever()Holds the calling thread until stopped; supply external frames from another thread or an independent input task

Compatible valid calibration is reused. An already-ready external input does not change startup waiting for other pending device inputs. Background startup returning does not mean calibration is complete. Keep feeding frames during preparation. The raw value returned by push_frame() is not the target algorithm result; read normal results from the corresponding target output after calibration takes effect. See External input.

Do not use has_next_task to decide whether to call sleep(): event-driven work can still advance while that field is false. Wait once and handle SleepResult.NO_TASKS. It can mean the SDK needs the next application-supplied frame, rather than calibration being finished. The snippet assumes the device is started and the application provides a new frame on each iteration.

import time
from qnbot_sdk import SleepResult

device.push_frame(frame)
update = glove.update()
result = update.sleep()
if result is SleepResult.STOPPED:
    pass  # End the application loop.
elif result is SleepResult.NO_TASKS:
    time.sleep(0.01)  # Continue with the next input frame.

Terminal requirements, failure, and cleanup

  • Pending background automatic preparation fails to start without an interactive console or when another interaction owns the terminal. Choose external for a service, custom UI, or non-interactive environment and submit confirmations from the application. This does not supply input frames for you.
  • Observe capture session_state and failure through capture_progress(), and target job.state and job.failure through calibration_progress(). Failure and cancellation states are FAILED and CANCELLED.
  • Handle preparation failure or cancellation reported after startup through these progress channels. Do not only wait for output or expect every failure to be thrown by startup. join() reports background runtime errors but does not replace capture/calibration progress handling.
  • Finish background use with request_stop() → join() → close(), reaching close even when an earlier step throws. Use try/finally for cleanup. Close after a manual loop as well.
  • Retained terminal guidance for manual updates and application-fed background preparation is released when preparation becomes ready, capture fails or is cancelled, or calibration fails. Background exit and participating device stop/close also release it. Foreground interaction lasts until that execution exits. A calibration cancellation notification does not guarantee cleanup is complete: still stop and close. The disappearance of prompts is not an output-readiness signal.

See the example index for complete external-input examples. They default to auto_terminal; add --capture-interaction external for application-owned prompts and confirmations.

SDK update notices at startup

Glove checks OTA at the first Glove start per SDK instance. The check looks for a newer stable version for the current binding and platform and waits at most one second. It prints the available version and upgrade link to standard error. It does not install updates. There is no separate Core check. If OTA is unreachable, normal startup continues. See Installation & requirements to upgrade.