| 1 |
Customize Initialization |
| 2 |
======================== |
| 3 |
|
| 4 |
Often we want to run custom code when we start up or tear down a scheduler or |
| 5 |
worker. We might do this manually with functions like ``Client.run`` or |
| 6 |
``Client.run_on_scheduler``, but this is error prone and difficult to automate. |
| 7 |
|
| 8 |
To resolve this, Dask includes a few mechanisms to run arbitrary code around |
| 9 |
the lifecycle of a Scheduler, Worker, Nanny, or Client. |
| 10 |
|
| 11 |
Preload Scripts |
| 12 |
--------------- |
| 13 |
|
| 14 |
Both ``dask-scheduler`` and ``dask-worker`` support a ``--preload`` option that |
| 15 |
allows custom initialization of each scheduler/worker respectively. A module or |
| 16 |
Python file passed as a ``--preload`` value is guaranteed to be imported before |
| 17 |
establishing any connection. A ``dask_setup(service)`` function is called if |
| 18 |
found, with a ``Scheduler``, ``Worker``, ``Nanny``, or ``Client`` instance as |
| 19 |
the argument. As the service stops, ``dask_teardown(service)`` is called if |
| 20 |
present. |
| 21 |
|
| 22 |
To support additional configuration, a single ``--preload`` module may register |
| 23 |
additional command-line arguments by exposing ``dask_setup`` as a Click_ |
| 24 |
command. This command will be used to parse additional arguments provided to |
| 25 |
``dask-worker`` or ``dask-scheduler`` and will be called before service |
| 26 |
initialization. |
| 27 |
|
| 28 |
.. _Click: http://click.pocoo.org/ |
| 29 |
|
| 30 |
Example |
| 31 |
~~~~~~~ |
| 32 |
|
| 33 |
As an example, consider the following file that creates a |
| 34 |
`scheduler plugin <https://distributed.dask.org/en/latest/plugins.html>`_ |
| 35 |
and registers it with the scheduler |
| 36 |
|
| 37 |
.. code-block:: python |
| 38 |
|
| 39 |
# scheduler-setup.py |
| 40 |
import click |
| 41 |
|
| 42 |
from distributed.diagnostics.plugin import SchedulerPlugin |
| 43 |
|
| 44 |
class MyPlugin(SchedulerPlugin): |
| 45 |
def __init__(self, print_count): |
| 46 |
self.print_count = print_count |
| 47 |
super().__init__() |
| 48 |
|
| 49 |
def add_worker(self, scheduler=None, worker=None, **kwargs): |
| 50 |
print("Added a new worker at:", worker) |
| 51 |
if self.print_count and scheduler is not None: |
| 52 |
print("Total workers:", len(scheduler.workers)) |
| 53 |
|
| 54 |
@click.command() |
| 55 |
@click.option("--print-count/--no-print-count", default=False) |
| 56 |
def dask_setup(scheduler, print_count): |
| 57 |
plugin = MyPlugin(print_count) |
| 58 |
scheduler.add_plugin(plugin) |
| 59 |
|
| 60 |
We can then run this preload script by referring to its filename (or module name |
| 61 |
if it is on the path) when we start the scheduler:: |
| 62 |
|
| 63 |
dask-scheduler --preload scheduler-setup.py --print-count |
| 64 |
|
| 65 |
Types |
| 66 |
~~~~~ |
| 67 |
|
| 68 |
Preloads can be specified as any of the following forms: |
| 69 |
|
| 70 |
- A path to a script, like ``/path/to/myfile.py`` |
| 71 |
- A module name that is on the path, like ``my_module.initialize`` |
| 72 |
- The text of a Python script, like ``import os; os.environ["A"] = "value"`` |
| 73 |
|
| 74 |
Configuration |
| 75 |
~~~~~~~~~~~~~ |
| 76 |
|
| 77 |
Preloads can also be registered with configuration at the following values: |
| 78 |
|
| 79 |
.. code-block:: yaml |
| 80 |
|
| 81 |
distributed: |
| 82 |
scheduler: |
| 83 |
preload: |
| 84 |
- "import os; os.environ['A'] = 'b'" # use Python text |
| 85 |
- /path/to/myfile.py # or a filename |
| 86 |
- my_module # or a module name |
| 87 |
preload-argv: |
| 88 |
- [] # Pass optional keywords |
| 89 |
- ["--option", "value"] |
| 90 |
- [] |
| 91 |
worker: |
| 92 |
preload: [] |
| 93 |
preload-argv: [] |
| 94 |
nanny: |
| 95 |
preload: [] |
| 96 |
preload-argv: [] |
| 97 |
client: |
| 98 |
preload: [] |
| 99 |
preload-argv: [] |
| 100 |
|
| 101 |
.. note:: |
| 102 |
|
| 103 |
Because the ``dask-worker`` command needs to accept keywords for both the |
| 104 |
Worker and the Nanny (if a nanny is used) it has both a ``--preload`` and |
| 105 |
``--preload-nanny`` keyword. All extra keywords (like ``--print-count`` |
| 106 |
above) will be sent to the workers rather than the nanny. There is no way |
| 107 |
to specify extra keywords to the nanny preload scripts on the command line. |
| 108 |
We recommend the use of the more flexible configuration if this is |
| 109 |
necessary. |
| 110 |
|
| 111 |
|
| 112 |
Worker Lifecycle Plugins |
| 113 |
------------------------ |
| 114 |
|
| 115 |
You can also create a class with ``setup``, ``teardown``, and ``transition`` methods, |
| 116 |
and register that class with the scheduler to give to every worker using the |
| 117 |
``Client.register_worker_plugin`` method. |
| 118 |
|
| 119 |
.. currentmodule:: distributed |
| 120 |
|
| 121 |
.. autosummary:: |
| 122 |
Client.register_worker_plugin |
| 123 |
|
| 124 |
.. automethod:: Client.register_worker_plugin |
| 125 |
:noindex: |