| 1 |
Python API |
| 2 |
========== |
| 3 |
|
| 4 |
You can create a ``dask.distributed`` scheduler by importing and creating a |
| 5 |
``Client`` with no arguments. This overrides whatever default was previously |
| 6 |
set. |
| 7 |
|
| 8 |
.. code-block:: python |
| 9 |
|
| 10 |
from dask.distributed import Client |
| 11 |
client = Client() |
| 12 |
|
| 13 |
You can navigate to ``http://localhost:8787/status`` to see the diagnostic |
| 14 |
dashboard if you have Bokeh installed. |
| 15 |
|
| 16 |
Client |
| 17 |
------ |
| 18 |
|
| 19 |
You can trivially set up a local cluster on your machine by instantiating a Dask |
| 20 |
Client with no arguments |
| 21 |
|
| 22 |
.. code-block:: python |
| 23 |
|
| 24 |
from dask.distributed import Client |
| 25 |
client = Client() |
| 26 |
|
| 27 |
This sets up a scheduler in your local process along with a number of workers and |
| 28 |
threads per worker related to the number of cores in your machine. |
| 29 |
|
| 30 |
If you want to run workers in your same process, you can pass the |
| 31 |
``processes=False`` keyword argument. |
| 32 |
|
| 33 |
.. code-block:: python |
| 34 |
|
| 35 |
client = Client(processes=False) |
| 36 |
|
| 37 |
This is sometimes preferable if you want to avoid inter-worker communication |
| 38 |
and your computations release the GIL. This is common when primarily using |
| 39 |
NumPy or Dask Array. |
| 40 |
|
| 41 |
|
| 42 |
LocalCluster |
| 43 |
------------ |
| 44 |
|
| 45 |
The ``Client()`` call described above is shorthand for creating a LocalCluster |
| 46 |
and then passing that to your client. |
| 47 |
|
| 48 |
.. code-block:: python |
| 49 |
|
| 50 |
from dask.distributed import Client, LocalCluster |
| 51 |
cluster = LocalCluster() |
| 52 |
client = Client(cluster) |
| 53 |
|
| 54 |
This is equivalent, but somewhat more explicit. |
| 55 |
|
| 56 |
You may want to look at the |
| 57 |
keyword arguments available on ``LocalCluster`` to understand the options available |
| 58 |
to you on handling the mixture of threads and processes, like specifying explicit |
| 59 |
ports, and so on. |
| 60 |
|
| 61 |
To create a local cluster with all workers running in dedicated subprocesses, |
| 62 |
``dask.distributed`` also offers the experimental ``SubprocessCluster``. |
| 63 |
|
| 64 |
Cluster manager features |
| 65 |
------------------------ |
| 66 |
|
| 67 |
Instantiating a cluster manager class like ``LocalCluster`` and then passing it to the |
| 68 |
``Client`` is a common pattern. Cluster managers also provide useful utilities to help |
| 69 |
you understand what is going on. |
| 70 |
|
| 71 |
For example you can retrieve the Dashboard URL. |
| 72 |
|
| 73 |
.. code-block:: python |
| 74 |
|
| 75 |
>>> cluster.dashboard_link |
| 76 |
'http://127.0.0.1:8787/status' |
| 77 |
|
| 78 |
You can retrieve logs from cluster components. |
| 79 |
|
| 80 |
.. code-block:: python |
| 81 |
|
| 82 |
>>> cluster.get_logs() |
| 83 |
{'Cluster': '', |
| 84 |
'Scheduler': "distributed.scheduler - INFO - Clear task state\ndistributed.scheduler - INFO - S... |
| 85 |
|
| 86 |
If you are using a cluster manager that supports scaling you can modify the number of workers manually |
| 87 |
or automatically based on workload. |
| 88 |
|
| 89 |
.. code-block:: python |
| 90 |
|
| 91 |
>>> cluster.scale(10) # Sets the number of workers to 10 |
| 92 |
|
| 93 |
>>> cluster.adapt(minimum=1, maximum=10) # Allows the cluster to auto scale to 10 when tasks are computed |
| 94 |
|
| 95 |
Reference |
| 96 |
--------- |
| 97 |
|
| 98 |
.. currentmodule:: distributed.deploy.local |
| 99 |
|
| 100 |
.. autoclass:: LocalCluster |
| 101 |
:members: |