| 1 |
Manage Environments |
| 2 |
=================== |
| 3 |
|
| 4 |
It is critical that each of your dask workers uses the same set of |
| 5 |
python packages and modules when executing your code, so that Dask |
| 6 |
can function. Upon connecting with a distributed ``Client``, Dask |
| 7 |
will automatically check the versions of some critical packages |
| 8 |
(including Dask itself) and warn you of any mismatch. |
| 9 |
|
| 10 |
Most functions you will run on Dask will require imports. This |
| 11 |
is even true for passing any object which is not a python builtin - |
| 12 |
the pickle serialisation method will save references to imported modules |
| 13 |
rather than trying to send all of your source code. |
| 14 |
|
| 15 |
You therefore must ensure that workers have access to all of the modules |
| 16 |
you will need, and ideally with exactly the same versions. |
| 17 |
|
| 18 |
Single-machine schedulers |
| 19 |
````````````````````````` |
| 20 |
|
| 21 |
If you are using the threaded scheduler, then you do not need to do |
| 22 |
anything, since the workers are in the same process, and objects are |
| 23 |
simply shared rather than serialised and deserialised. |
| 24 |
|
| 25 |
Similarly, if you use the multiprocessing scheduler, new processes |
| 26 |
will be copied from, or launched in the same way as the original process, |
| 27 |
so you only need make sure that you have not changed environment variables |
| 28 |
related to starting up python and importing |
| 29 |
code (such as PATH, PYTHONPATH, ``sys.path``). |
| 30 |
|
| 31 |
If you are using the distributed scheduler on a single machine, this is roughly |
| 32 |
equivalent to using the multiprocessing scheduler, above, if you are launching |
| 33 |
with ``Client(...)`` or ``LocalCluster(...)``. |
| 34 |
|
| 35 |
However, if you are launching your workers from the command line, then you must |
| 36 |
ensure that you are running in the same environment (virtualenv, pipenv or conda). |
| 37 |
|
| 38 |
The rest of this page concerns only distributed clusters. |
| 39 |
|
| 40 |
Maintain consistent environments |
| 41 |
```````````````````````````````` |
| 42 |
|
| 43 |
If you manage your environments yourself, then setting up module consistency |
| 44 |
can be as simple as creating environments from the same pip or conda specification |
| 45 |
on each machine. You should consult the documentation for ``pip``, ``pipenv`` |
| 46 |
and ``conda``, whichever you normally use. You will normally want to be as specific |
| 47 |
about package versions as possible, and distribute the same environment file to |
| 48 |
workers before installation. |
| 49 |
|
| 50 |
However, other common ways to distribute an environment directly, rather than build it |
| 51 |
in-place, include: |
| 52 |
|
| 53 |
- docker images, where the environment has been built into the image; this is the |
| 54 |
normal route when you are running on infrastructure enabled by docker, such as |
| 55 |
kubernetes |
| 56 |
- `conda-pack`_ is a tool for bundling existing conda environments, so they can be |
| 57 |
relocated to other machines. This tool was specifically created for dask on YARN/hadoop |
| 58 |
clusters, but could be used elsewhere |
| 59 |
- shared filesystem, e.g., NFS, that can be seen by all machines. Note that importing |
| 60 |
python modules is fairly IO intensive, so your server needs to be able to handle |
| 61 |
many requests |
| 62 |
- cluster install method (e.g., `parcels`_): depending on your infrastructure, there may be |
| 63 |
ways to install specific binaries to all workers in a cluster. |
| 64 |
|
| 65 |
.. _conda-pack: https://conda.github.io/conda-pack/ |
| 66 |
.. _parcels: https://docs.cloudera.com/documentation/enterprise/latest/topics/cm_ig_parcels.html |
| 67 |
|
| 68 |
Temporary installations |
| 69 |
``````````````````````` |
| 70 |
The worker plugin :class:`distributed.diagnostics.plugin.PipInstall` allows you to |
| 71 |
run pip installation commands on your workers, and optionally have them restart |
| 72 |
upon success. Please read the plugin documentation to see how to use this. |
| 73 |
|
| 74 |
Objects in ``__main__`` |
| 75 |
``````````````````````` |
| 76 |
|
| 77 |
Objects that you create without any reference to modules, such as classes that |
| 78 |
are defined right in the repl or notebook cells, are not pickled with reference to |
| 79 |
any imported module. You can redefine such objects, and Dask will serialise them |
| 80 |
completely, including the source code. This can be a good way to try new things |
| 81 |
while working with a distributed setup. |
| 82 |
|
| 83 |
Send Source |
| 84 |
``````````` |
| 85 |
|
| 86 |
Particularly during development, you may want to send files directly to workers |
| 87 |
that are already running. |
| 88 |
|
| 89 |
You should use ``client.upload_file`` in these cases. |
| 90 |
For more detail, see the `API docs`_ and a |
| 91 |
StackOverflow question |
| 92 |
`"Can I use functions imported from .py files in Dask/Distributed?"`__ |
| 93 |
This function supports both standalone file and setuptools's ``.egg`` files |
| 94 |
for larger modules. |
| 95 |
|
| 96 |
__ http://stackoverflow.com/questions/39295200/can-i-use-functions-imported-from-py-files-in-dask-distributed |
| 97 |
.. _API docs: https://distributed.readthedocs.io/en/latest/api.html#distributed.executor.Executor.upload_file |