quantlab.utils.progress.CancelToken#
- class quantlab.utils.progress.CancelToken[source]#
Bases:
objectCooperative stop signal that a long loop checks between steps.
“Cooperative” means the loop is not interrupted; it checks the token at safe points (between batches or between time windows) and stops there. Create a token, pass it to a run, and call
cancel()from any thread. At its next check the run emits acancelledevent and stops; work already in progress is allowed to finish. The token wraps athreading.Event, socancel()is thread-safe and calling it twice is harmless.Cancellation is deliberately separate from the handling of an exhausted vendor quota. That handling may wait and then resume the run, which is the opposite of what a user who pressed cancel wants.
Examples
>>> token = CancelToken() >>> token.is_cancelled() False >>> token.cancel() >>> token.is_cancelled() True
- cancel()[source]#
Request a stop. Safe to call more than once and from any thread.
Examples
>>> token = CancelToken() >>> token.cancel() >>> token CancelToken(cancelled=True)
- is_cancelled()[source]#
Return whether
cancel()has been called since the last reset.Examples
>>> token = CancelToken() >>> token.is_cancelled() False
- reset()[source]#
Clear the signal so the token can be reused for a new run.
The loops never call this themselves. The token belongs to the caller; if a run cleared it on start, a cancel issued just before the run began would be silently lost.
Examples
>>> token = CancelToken() >>> token.cancel() >>> token.reset() >>> token.is_cancelled() False