Skip to main content

What changes for your tasks

Most tasks run with the ray runner without a change. The differences on this page all come from one fact: the task runs in another process, and often on another machine, than your script.

The task's code must be installed on the workers​

A worker imports your task function from the packages installed on its node. Install the packages that define your tasks, and the connection plugins they use, on every node that runs workers.

Functions defined in the script itself or in a notebook are sent with the task, so they need no installation.

The task and its arguments must be serializable​

The runner sends the task function and its arguments to the worker as bytes. Python calls this serializing. Most values can be serialized: strings, numbers, lists, dictionaries, dataclasses.

Some values cannot: an open file, a lock, an open database connection. When an argument cannot be serialized, the run fails with TaskNotSerializableError before any host starts.

import threading

lock = threading.Lock()
nr.run(task=my_task, lock=lock) # fails with TaskNotSerializableError

Create such values inside the task instead of passing them as arguments.

The task works on a copy of the host​

The worker receives a copy of the host. What reaches the driver's inventory after the task depends on what the task changed:

The task changesResult on the driver
host.data, for example task.host["os_version"] = "17.3"The driver's inventory receives the new data, as with the built-in runners
Other parts of the host: hostname, platform, groups, defaultsThe change stays on the worker
host.data with a value that cannot be serializedThe data is not sent back. The worker logs a warning

Variables of your script are not shared​

A task that adds to a list or a dictionary defined in the script changes the worker's copy, not the script's. Return the value in the task's Result instead.

collected = []


def collect(task):
collected.append(task.host.name) # changes a copy on the worker
return Result(host=task.host, result=task.host.name) # reaches the driver

Operation tasks need an entry point​

Nornir Operations select a task for each platform from a registry. Workers have their own registry, filled from the packages installed on their node. See Run Operations.

Long sync tasks should call task.checkpoint()​

A cancelled sync task stops at its next task.checkpoint(). The start of a subtask and each yield of a task that reports progress are checkpoints too. A long task that reaches no checkpoint cannot be stopped early. See Deadlines and cancellation.

Processors see stand-in subtasks​

Processors run on the driver and receive the same events as with other runners. For subtasks, they receive a stand-in task object that has the subtask's name, severity level and parameters. Three details differ from ThreadedRunner:

  • the stand-in's task attribute is not your function;
  • subtask parameters that cannot be serialized are replaced by an empty dictionary;
  • the processor is called when the event reaches the driver, a short time after the subtask started or ended on the worker.