Sessions stay open between runs
Opening a session to a network device is often the slowest part of a task: the TCP connection, the SSH key exchange and the login can take longer than the command itself. nornir-ray keeps the session open on the worker, so a second run for the same host does not open it again.
When this helps
Session reuse helps when one Python process runs tasks several times on the same hosts:
- a notebook where you run one cell after another;
- a service that stays running and runs tasks on request;
- a script that calls
nr.runseveral times.
It does not help between two separate starts of a script on one machine with a local Ray, because the workers stop when the script exits.
How it works
Two rules make this possible:
- A host always goes to the same worker. The runner computes the worker from the host's name and the list of workers. The result is the same at every run while the list of workers is the same.
- A worker keeps each host it has run. It keeps the host object with its open sessions. The next call for that host uses the kept object.
When a worker opens a new session
| Event | Effect on sessions |
|---|---|
| The host's inventory data changed on the driver, for example a new password | The worker closes the host's old sessions and opens new ones |
| A sync task for the host passed its deadline | The host's next run opens new sessions |
| The host's worker stopped or was replaced | The host's next run opens new sessions |
| A worker was added or removed | Only the hosts that move to or from that worker open new sessions |
| The runner was closed, or the script exited | All sessions close |
Cancelling a run keeps the workers and their sessions.
The technical details are in Session reuse and Host assignment.