Dask’s plugin system enables you to run custom Python code for certain events. You can use plugins that are specific to schedulers, workers, or nannies. A worker plugin, for example, allows you to run custom Python code on all your workers at certain event in the worker’s lifecycle (e.g. when the worker process is started). In each section below, you’ll see how to create your own plugin or use a Dask-provided built-in plugin.
- class distributed.diagnostics.plugin.SchedulerPlugin¶
Interface to extend the Scheduler
The scheduler operates by triggering and responding to events like
A plugin enables custom code to run at each of those same events. The scheduler will run the analogous methods on this class when each event is triggered. This runs user code within the scheduler thread that can perform arbitrary operations in synchrony with the scheduler itself.
Plugins are often used for diagnostics and measurement, but have full access to the scheduler and could in principle affect core scheduling.
To implement a plugin:
subclass this class
override some of its methods
add the plugin to the scheduler with
>>> class Counter(SchedulerPlugin): ... def __init__(self): ... self.counter = 0 ... ... def transition(self, key, start, finish, *args, **kwargs): ... if start == 'processing' and finish == 'memory': ... self.counter += 1 ... ... def restart(self, scheduler): ... self.counter = 0
>>> plugin = Counter() >>> scheduler.add_plugin(plugin)
- add_worker(scheduler: Scheduler, worker: str) None | Awaitable[None] ¶
Run when a new worker enters the cluster
- async close() None ¶
Run when the scheduler closes down
This runs at the beginning of the Scheduler shutdown process, but after workers have been asked to shut down gracefully
- remove_worker(scheduler: Scheduler, worker: str) None | Awaitable[None] ¶
Run when a worker leaves the cluster
- async start(scheduler: Scheduler) None ¶
Run when the scheduler starts up
This runs at the end of the Scheduler startup process
- transition(key: str, start: str, finish: str, *args: Any, **kwargs: Any) None ¶
Run whenever a task changes state
Start state of the transition. One of released, waiting, processing, memory, error.
Final state of the transition.
- *args, **kwargs
More options passed when transitioning This may include worker ID, compute time, etc.
RabbitMQ is a distributed messaging queue that we can use to post updates about task transitions. By posting transitions to RabbitMQ, we allow other machines to do the processing of transitions and keep scheduler processing to a minimum. See the RabbitMQ tutorial for more information on RabbitMQ and how to consume the messages.
import json from distributed.diagnostics.plugin import SchedulerPlugin import pika class RabbitMQPlugin(SchedulerPlugin): def __init__(self): # Update host to be your RabbitMQ host self.connection = pika.BlockingConnection( pika.ConnectionParameters(host='localhost')) self.channel = self.connection.channel() self.channel.queue_declare(queue='dask_task_status', durable=True) def transition(self, key, start, finish, *args, **kwargs): message = dict( key=key, start=start, finish=finish, ) self.channel.basic_publish( exchange='', routing_key='dask_task_status', body=json.dumps(message), properties=pika.BasicProperties( delivery_mode=2, # make message persistent )) @click.command() def dask_setup(scheduler): plugin = RabbitMQPlugin() scheduler.add_plugin(plugin)
dask-scheduler --preload <filename.py>
Accessing Full Task State¶
If you would like to access the full
stored in the scheduler you can do this by passing and storing a reference to
the scheduler as so:
from distributed.diagnostics.plugin import SchedulerPlugin class MyPlugin(SchedulerPlugin): def __init__(self, scheduler): self.scheduler = scheduler def transition(self, key, start, finish, *args, **kwargs): # Get full TaskState ts = self.scheduler.tasks[key] @click.command() def dask_setup(scheduler): plugin = MyPlugin(scheduler) scheduler.add_plugin(plugin)
Watch the video below for an example using a
WorkerPlugin to add a
- class distributed.diagnostics.plugin.WorkerPlugin¶
Interface to extend the Worker
A worker plugin enables custom code to run at different stages of the Workers’ lifecycle: at setup, during task state transitions, when a task or dependency is released, and at teardown.
A plugin enables custom code to run at each of step of a Workers’s life. Whenever such an event happens, the corresponding method on this class will be called. Note that the user code always runs within the Worker’s main thread.
To implement a plugin implement some of the methods of this class and register the plugin to your client in order to have it attached to every existing and future workers with
>>> class ErrorLogger(WorkerPlugin): ... def __init__(self, logger): ... self.logger = logger ... ... def setup(self, worker): ... self.worker = worker ... ... def transition(self, key, start, finish, *args, **kwargs): ... if finish == 'error': ... ts = self.worker.tasks[key] ... exc_info = (type(ts.exception), ts.exception, ts.traceback) ... self.logger.error( ... "Error during computation of '%s'.", key, ... exc_info=exc_info ... )
>>> import logging >>> plugin = ErrorLogger(logging) >>> client.register_worker_plugin(plugin)
Run when the plugin is attached to a worker. This happens when the plugin is registered and attached to existing workers, or when a worker is created after the plugin has been registered.
Run when the worker to which the plugin is attached to is closed
- transition(key, start, finish, **kwargs)¶
Throughout the lifecycle of a task (see Worker), Workers are instructed by the scheduler to compute certain tasks, resulting in transitions in the state of each task. The Worker owning the task is then notified of this state transition.
Whenever a task changes its state, this method will be called.
Start state of the transition. One of waiting, ready, executing, long-running, memory, error.
Final state of the transition.
- kwargsMore options passed when transitioning
Built-In Worker Plugins¶
- class distributed.diagnostics.plugin.PipInstall(packages, pip_options=None, restart=False)¶
A Worker Plugin to pip install a set of packages
This accepts a set of packages to install on all workers. You can also optionally ask for the worker to restart itself after performing this installation.
This will increase the time it takes to start up each worker. If possible, we recommend including the libraries in the worker environment or image. This is primarily intended for experimentation and debugging.
Additional issues may arise if multiple workers share the same file system. Each worker might try to install the packages simultaneously.
A list of strings to place after “pip install” command
Additional options to pass to pip.
- restartbool, default False
Whether or not to restart the worker after pip installing Only functions if the worker has an attached nanny process
>>> from dask.distributed import PipInstall >>> plugin = PipInstall(packages=["scikit-learn"], pip_options=["--upgrade"])
- class distributed.diagnostics.plugin.UploadFile(filepath)¶
A WorkerPlugin to upload a local file to workers.
- filepath: str
A path to the file (.py, egg, or zip) to upload
>>> from distributed.diagnostics.plugin import UploadFile
- class distributed.diagnostics.plugin.NannyPlugin¶
Interface to extend the Nanny
A worker plugin enables custom code to run at different stages of the Workers’ lifecycle. A nanny plugin does the same thing, but benefits from being able to run code before the worker is started, or to restart the worker if necessary.
To implement a plugin implement some of the methods of this class and register the plugin to your client in order to have it attached to every existing and future nanny by passing
restartattribute is used to control whether or not a running
Workerneeds to be restarted when registering the plugin.
Run when the plugin is attached to a nanny. This happens when the plugin is registered and attached to existing nannies, or when a nanny is created after the plugin has been registered.
Run when the nanny to which the plugin is attached to is closed
Built-In Nanny Plugins¶
- class distributed.diagnostics.plugin.UploadDirectory(path, restart=False, update_path=False, skip_words=('.git', '.github', '.pytest_cache', 'tests', 'docs'), skip=(<function UploadDirectory.<lambda>>, ))¶
A NannyPlugin to upload a local file to workers.
- path: str
A path to the directory to upload
>>> from distributed.diagnostics.plugin import UploadDirectory >>> client.register_worker_plugin(UploadDirectory("/path/to/directory"), nanny=True)