| 1 |
Python API (advanced) |
| 2 |
===================== |
| 3 |
|
| 4 |
.. currentmodule:: distributed |
| 5 |
|
| 6 |
In some rare cases, experts may want to create ``Scheduler``, ``Worker``, and |
| 7 |
``Nanny`` objects explicitly in Python. This is often necessary when making |
| 8 |
tools to automatically deploy Dask in custom settings. |
| 9 |
|
| 10 |
It is more common to create a :doc:`Local cluster with Client() on a single |
| 11 |
machine <deploying-python>` or use the :doc:`Command Line Interface (CLI) <deploying-cli>`. |
| 12 |
New readers are recommended to start there. |
| 13 |
|
| 14 |
If you do want to start Scheduler and Worker objects yourself you should be a |
| 15 |
little familiar with ``async``/``await`` style Python syntax. These objects |
| 16 |
are awaitable and are commonly used within ``async with`` context managers. |
| 17 |
Here are a few examples to show a few ways to start and finish things. |
| 18 |
|
| 19 |
Full Example |
| 20 |
------------ |
| 21 |
|
| 22 |
.. autosummary:: |
| 23 |
Scheduler |
| 24 |
Worker |
| 25 |
Client |
| 26 |
|
| 27 |
We first start with a comprehensive example of setting up a Scheduler, two Workers, |
| 28 |
and one Client in the same event loop, running a simple computation, and then |
| 29 |
cleaning everything up. |
| 30 |
|
| 31 |
.. code-block:: python |
| 32 |
|
| 33 |
import asyncio |
| 34 |
from dask.distributed import Scheduler, Worker, Client |
| 35 |
|
| 36 |
async def f(): |
| 37 |
async with Scheduler() as s: |
| 38 |
async with Worker(s.address) as w1, Worker(s.address) as w2: |
| 39 |
async with Client(s.address, asynchronous=True) as client: |
| 40 |
future = client.submit(lambda x: x + 1, 10) |
| 41 |
result = await future |
| 42 |
print(result) |
| 43 |
|
| 44 |
asyncio.get_event_loop().run_until_complete(f()) |
| 45 |
|
| 46 |
Now we look at simpler examples that build up to this case. |
| 47 |
|
| 48 |
Scheduler |
| 49 |
--------- |
| 50 |
|
| 51 |
.. autosummary:: |
| 52 |
Scheduler |
| 53 |
|
| 54 |
We create scheduler by creating a ``Scheduler()`` object, and then ``await`` |
| 55 |
that object to wait for it to start up. We can then wait on the ``.finished`` |
| 56 |
method to wait until it closes. In the meantime the scheduler will be active |
| 57 |
managing the cluster.. |
| 58 |
|
| 59 |
.. code-block:: python |
| 60 |
|
| 61 |
import asyncio |
| 62 |
from dask.distributed import Scheduler, Worker |
| 63 |
|
| 64 |
async def f(): |
| 65 |
s = Scheduler() # scheduler created, but not yet running |
| 66 |
s = await s # the scheduler is running |
| 67 |
await s.finished() # wait until the scheduler closes |
| 68 |
|
| 69 |
asyncio.get_event_loop().run_until_complete(f()) |
| 70 |
|
| 71 |
This program will run forever, or until some external process connects to the |
| 72 |
scheduler and tells it to stop. If you want to close things yourself you can |
| 73 |
close any ``Scheduler``, ``Worker``, ``Nanny``, or ``Client`` class by awaiting |
| 74 |
the ``.close`` method: |
| 75 |
|
| 76 |
.. code-block:: python |
| 77 |
|
| 78 |
await s.close() |
| 79 |
|
| 80 |
|
| 81 |
Worker |
| 82 |
------ |
| 83 |
|
| 84 |
.. autosummary:: |
| 85 |
Worker |
| 86 |
|
| 87 |
The worker follows the same API. |
| 88 |
The only difference is that the worker needs to know the address of the |
| 89 |
scheduler. |
| 90 |
|
| 91 |
.. code-block:: python |
| 92 |
|
| 93 |
import asyncio |
| 94 |
from dask.distributed import Scheduler, Worker |
| 95 |
|
| 96 |
async def f(scheduler_address): |
| 97 |
w = await Worker(scheduler_address) |
| 98 |
await w.finished() |
| 99 |
|
| 100 |
asyncio.get_event_loop().run_until_complete(f("tcp://127.0.0.1:8786")) |
| 101 |
|
| 102 |
|
| 103 |
Start many in one event loop |
| 104 |
---------------------------- |
| 105 |
|
| 106 |
.. autosummary:: |
| 107 |
Scheduler |
| 108 |
Worker |
| 109 |
|
| 110 |
We can run as many of these objects as we like in the same event loop. |
| 111 |
|
| 112 |
.. code-block:: python |
| 113 |
|
| 114 |
import asyncio |
| 115 |
from dask.distributed import Scheduler, Worker |
| 116 |
|
| 117 |
async def f(): |
| 118 |
s = await Scheduler() |
| 119 |
w = await Worker(s.address) |
| 120 |
await w.finished() |
| 121 |
await s.finished() |
| 122 |
|
| 123 |
asyncio.get_event_loop().run_until_complete(f()) |
| 124 |
|
| 125 |
|
| 126 |
Use Context Managers |
| 127 |
-------------------- |
| 128 |
|
| 129 |
We can also use ``async with`` context managers to make sure that we clean up |
| 130 |
properly. Here is the same example as from above: |
| 131 |
|
| 132 |
.. code-block:: python |
| 133 |
|
| 134 |
import asyncio |
| 135 |
from dask.distributed import Scheduler, Worker |
| 136 |
|
| 137 |
async def f(): |
| 138 |
async with Scheduler() as s: |
| 139 |
async with Worker(s.address) as w: |
| 140 |
await w.finished() |
| 141 |
await s.finished() |
| 142 |
|
| 143 |
asyncio.get_event_loop().run_until_complete(f()) |
| 144 |
|
| 145 |
Alternatively, in the example below we also include a ``Client``, run a small |
| 146 |
computation, and then allow things to clean up after that computation.. |
| 147 |
|
| 148 |
.. code-block:: python |
| 149 |
|
| 150 |
import asyncio |
| 151 |
from dask.distributed import Scheduler, Worker, Client |
| 152 |
|
| 153 |
async def f(): |
| 154 |
async with Scheduler() as s: |
| 155 |
async with Worker(s.address) as w1, Worker(s.address) as w2: |
| 156 |
async with Client(s.address, asynchronous=True) as client: |
| 157 |
future = client.submit(lambda x: x + 1, 10) |
| 158 |
result = await future |
| 159 |
print(result) |
| 160 |
|
| 161 |
asyncio.get_event_loop().run_until_complete(f()) |
| 162 |
|
| 163 |
This is equivalent to creating and ``awaiting`` each server, and then calling |
| 164 |
``.close`` on each as we leave the context. |
| 165 |
In this example we don't wait on ``s.finished()``, so this will terminate |
| 166 |
relatively quickly. You could have called ``await s.finished()`` though if you |
| 167 |
wanted this to run forever. |
| 168 |
|
| 169 |
.. _nanny: |
| 170 |
|
| 171 |
Nanny |
| 172 |
----- |
| 173 |
|
| 174 |
.. autosummary:: |
| 175 |
Nanny |
| 176 |
|
| 177 |
Alternatively, we can replace ``Worker`` with ``Nanny`` if we want your workers |
| 178 |
to be managed in a separate process. The ``Nanny`` constructor follows the |
| 179 |
same API. This allows workers to restart themselves in case of failure. Also, |
| 180 |
it provides some additional monitoring, and is useful when coordinating many |
| 181 |
workers that should live in different processes in order to avoid the GIL_. |
| 182 |
|
| 183 |
.. code-block:: python |
| 184 |
|
| 185 |
# w = await Worker(s.address) |
| 186 |
w = await Nanny(s.address) |
| 187 |
|
| 188 |
.. _GIL: https://docs.python.org/3/glossary.html#term-gil |
| 189 |
|
| 190 |
|
| 191 |
API |
| 192 |
--- |
| 193 |
|
| 194 |
These classes have a variety of keyword arguments that you can use to control |
| 195 |
their behavior. See the API documentation below for more information. |
| 196 |
|
| 197 |
Scheduler |
| 198 |
~~~~~~~~~ |
| 199 |
.. autoclass:: Scheduler |
| 200 |
:members: |
| 201 |
|
| 202 |
Worker |
| 203 |
~~~~~~ |
| 204 |
|
| 205 |
.. autoclass:: Worker |
| 206 |
:members: |
| 207 |
|
| 208 |
Nanny |
| 209 |
~~~~~ |
| 210 |
|
| 211 |
.. autoclass:: Nanny |
| 212 |
:members: |