Deadlines and cancellation
Deadlines and cancellation follow the rules of Nornir's built-in runners. This page describes what you observe, and what happens to the workers and the sessions.
Set a deadline
| Option | Limits |
|---|---|
per_host_timeout | The seconds each host may run |
timeout | The seconds the whole run may take |
nr = nr.with_runner(RayRunner(per_host_timeout=30, timeout=600))
A host that passes its deadline fails with Nornir's TaskTimeoutError. The other hosts
continue.
A host's deadline starts when the driver starts the host. It keeps running while the host
waits for a free place on a busy worker. With many more hosts than places, set
per_host_timeout with this waiting time in mind, or raise workers_per_node or
hosts_per_worker.
Cancel a run
Start the run in the background, then cancel it:
handle = nr.submit(task=my_task)
# later
handle.cancel()
- An async task stops at the call it is awaiting.
- A sync task stops at its next checkpoint. Checkpoints are calls to
task.checkpoint(), the start of each subtask, and eachyieldof a task that reports progress.
Cancelling keeps the workers and the hosts' sessions. The next run uses them again.
Make a long sync task stoppable
Python cannot stop a thread from outside. A sync task must reach a checkpoint to stop. In a
long loop, call task.checkpoint():
def collect_many(task, commands):
outputs = []
for command in commands:
task.checkpoint() # the task stops here when the run is cancelled
outputs.append(send(task.host, command))
return Result(host=task.host, result=outputs)
What happens to a sync task that does not stop
| Situation | What you observe | What happens on the worker |
|---|---|---|
| The run is cancelled and the task reaches a checkpoint within 5 seconds | The host is recorded as cancelled, with the results of the subtasks completed before | The task stops. The session stays open |
| The run is cancelled and the task reaches no checkpoint within 5 seconds | The host is recorded as cancelled | The call is abandoned (see below) |
| The host passes its deadline | The host fails with TaskTimeoutError at once | The call is abandoned (see below) |
An abandoned call is a sync task that the driver stopped waiting for while its thread still runs on the worker. For an abandoned call, the worker:
- discards whatever the task produces later;
- forgets the host's open sessions, so the host's next run opens new ones;
- closes the old sessions when the task's thread ends.
The other hosts of that worker keep their sessions.
A worker has spare threads for abandoned calls, so they do not reduce the number of hosts it
runs. When a worker holds more than max_abandoned_calls abandoned calls that are still
running, 4 by default, the runner replaces the worker at the start of the next run. The hosts
of a replaced worker open new sessions.
Stop a script with Ctrl-C
When you interrupt a sync run with Ctrl-C, the runner tells the workers to cancel the calls of that run. The tasks stop at their next checkpoint.