Debian Codegraph @

docs/source/custom-graphs.rst

raw
1 Custom Graphs
2 =============
3
4 There may be times when you want to do parallel computing but your application
5 doesn't fit neatly into something like Dask Array or Dask Bag. In these
6 cases, you can interact directly with the Dask schedulers. These schedulers
7 operate well as standalone modules.
8
9 This separation provides a release valve for complex situations and allows
10 advanced projects to have additional opportunities for parallel execution, even if
11 those projects have an internal representation for their computations. As Dask
12 schedulers improve or expand to distributed memory, code written to use Dask
13 schedulers will advance as well.
14
15 .. _custom-graph-example:
16
17 Example
18 -------
19
20 .. figure:: images/pipeline.svg
21 :alt: "Dask graph for data pipeline"
22 :align: right
23
24 As discussed in the :doc:`motivation <graphs>` and :doc:`specification <spec>`
25 sections, the schedulers take a task graph (which is a dict of tuples of
26 functions) and a list of desired keys from that graph.
27
28 Here is a mocked out example building a graph for a traditional clean and
29 analyze pipeline:
30
31 .. code-block:: python
32
33 def load(filename):
34 ...
35
36 def clean(data):
37 ...
38
39 def analyze(sequence_of_data):
40 ...
41
42 def store(result):
43 with open(..., 'w') as f:
44 f.write(result)
45
46 dsk = {'load-1': (load, 'myfile.a.data'),
47 'load-2': (load, 'myfile.b.data'),
48 'load-3': (load, 'myfile.c.data'),
49 'clean-1': (clean, 'load-1'),
50 'clean-2': (clean, 'load-2'),
51 'clean-3': (clean, 'load-3'),
52 'analyze': (analyze, ['clean-%d' % i for i in [1, 2, 3]]),
53 'store': (store, 'analyze')}
54
55 from dask.threaded import get
56 get(dsk, 'store') # executes in parallel
57
58
59 Keyword arguments in custom Dask graphs
60 ---------------------------------------
61
62 Sometimes, you may want to pass keyword arguments to a function
63 in a custom Dask graph.
64 You can do that using the :func:`dask.utils.apply` function, like this:
65
66 .. code-block:: python
67
68 from dask.utils import apply
69
70 task = (apply, func, args, kwargs) # equivalent to func(*args, **kwargs)
71
72 dsk = {'task-name': task,
73 ...
74 }
75
76
77 In the example above:
78
79 - ``args`` should be a tuple (eg: ``(arg_1, arg_2, arg_3)``), and
80 - ``kwargs`` should be a dictionary (eg: ``{"kwarg_1": value, "kwarg_2": value}``
81
82
83 Related Projects
84 ----------------
85
86 The following excellent projects also provide parallel execution:
87
88 * Joblib_
89 * Multiprocessing_
90 * `IPython Parallel`_
91 * `Concurrent.futures`_
92 * `Luigi`_
93
94 Each library lets you dictate how your tasks relate to each other with various
95 levels of sophistication. Each library executes those tasks with some internal
96 logic.
97
98 Dask schedulers differ in the following ways:
99
100 1. You specify the entire graph as a Python dict rather than using a
101 specialized API.
102 2. You get a variety of schedulers, ranging from a single-machine, single-core
103 scheduler to threaded, multi-process, and distributed options.
104 3. You benefit from logic to execute the graph in a way that minimizes memory
105 footprint with the Dask single-machine schedulers.
106
107 But the other projects offer different advantages and different programming
108 paradigms. One should inspect all such projects before selecting one.
109
110 .. _Joblib: https://joblib.readthedocs.io/en/latest/
111 .. _Multiprocessing: https://docs.python.org/3/library/multiprocessing.html
112 .. _`IPython Parallel`: https://ipyparallel.readthedocs.io/en/latest/
113 .. _`Concurrent.futures`: https://docs.python.org/3/library/concurrent.futures.html
114 .. _Luigi: https://luigi.readthedocs.io