File: dagpool.rst

package info (click to toggle)
python-eventlet 0.26.1-7%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,916 kB
  • sloc: python: 24,898; makefile: 98
file content (493 lines) | stat: -rw-r--r-- 15,603 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
:mod:`dagpool` -- Dependency-Driven Greenthreads
================================================

Rationale
*********

The dagpool module provides the :class:`DAGPool <eventlet.dagpool.DAGPool>`
class, which addresses situations in which the value produced by one
greenthread might be consumed by several others -- while at the same time a
consuming greenthread might depend on the output from several different
greenthreads.

If you have a tree with strict many-to-one dependencies -- each producer
greenthread provides results to exactly one consumer, though a given consumer
may depend on multiple producers -- that could be addressed by recursively
constructing a :class:`GreenPool <eventlet.greenpool.GreenPool>` of producers
for each consumer, then :meth:`waiting <eventlet.greenpool.GreenPool.waitall>`
for all producers.

If you have a tree with strict one-to-many dependencies -- each consumer
greenthread depends on exactly one producer, though a given producer may
provide results to multiple consumers -- that could be addressed by causing
each producer to finish by launching a :class:`GreenPool
<eventlet.greenpool.GreenPool>` of consumers.

But when you have many-to-many dependencies, a tree doesn't suffice. This is
known as a
`Directed Acyclic Graph <https://en.wikipedia.org/wiki/Directed_acyclic_graph>`_,
or DAG.

You might consider sorting the greenthreads into dependency order
(`topological sort <https://en.wikipedia.org/wiki/Topological_sorting>`_) and
launching them in a GreenPool. But the concurrency of the GreenPool must be
strictly constrained to ensure that no greenthread is launched before all its
upstream producers have completed -- and the appropriate pool size is
data-dependent. Only a pool of size 1 (serializing all the greenthreads)
guarantees that a topological sort will produce correct results.

Even if you do serialize all the greenthreads, how do you pass results from
each producer to all its consumers, which might start at very different points
in time?

One answer is to associate each greenthread with a distinct key, and store its
result in a common dict. Then each consumer greenthread can identify its
direct upstream producers by their keys, and find their results in that dict.

This is the essence of DAGPool.

A DAGPool instance owns a dict, and stores greenthread results in that dict.
You :meth:`spawn <eventlet.dagpool.DAGPool.spawn>` *all* greenthreads in the
DAG, specifying for each its own key -- the key with which its result will be
stored on completion -- plus the keys of the upstream producer greenthreads on
whose results it directly depends.

Keys need only be unique within the DAGPool instance; they need not be UUIDs.
A key can be any type that can be used as a dict key. String keys make it
easier to reason about a DAGPool's behavior, but are by no means required.

The DAGPool passes to each greenthread an iterable of (key, value) pairs.
The key in each pair is the key of one of the greenthread's specified upstream
producers; the value is the value returned by that producer greenthread. Pairs
are delivered in the order results become available; the consuming greenthread
blocks until the next result can be delivered.

Tutorial
*******
Example
-------

Consider a couple of programs in some compiled language that depend on a set
of precompiled libraries. Suppose every such build requires as input the
specific set of library builds on which it directly depends.

::

    a  zlib
    | /  |
    |/   |
    b    c
    |   /|
    |  / |
    | /  |
    |/   |
    d    e

We can't run the build for program d until we have the build results for both
b and c. We can't run the build for library b until we have build results for
a and zlib. We can, however, immediately run the builds for a and zlib.

So we can use a DAGPool instance to spawn greenthreads running a function such
as this:

::

    def builder(key, upstream):
        for libname, product in upstream:
            # ... configure build for 'key' to use 'product' for 'libname'
        # all upstream builds have completed
        # ... run build for 'key'
        return build_product_for_key

:meth:`spawn <eventlet.dagpool.DAGPool.spawn>` all these greenthreads:

::

    pool = DAGPool()
    # the upstream producer keys passed to spawn() can be from any iterable,
    # including a generator
    pool.spawn("d", ("b", "c"), builder)
    pool.spawn("e", ["c"], builder)
    pool.spawn("b", ("a", "zlib"), builder)
    pool.spawn("c", ["zlib"], builder)
    pool.spawn("a", (), builder)

As with :func:`eventlet.spawn() <eventlet.spawn>`, if you need to pass special
build flags to some set of builds, these can be passed as either positional or
keyword arguments:

::

    def builder(key, upstream, cflags="", linkflags=""):
        ...

    pool.spawn("d", ("b", "c"), builder, "-o2")
    pool.spawn("e", ["c"], builder, linkflags="-pie")

However, if the arguments to each builder() call are uniform (as in the
original example), you could alternatively build a dict of the dependencies
and call :meth:`spawn_many() <eventlet.dagpool.DAGPool.spawn_many>`:

::

    deps = dict(d=("b", "c"),
                e=["c"],
                b=("a", "zlib"),
                c=["zlib"],
                a=())
    pool.spawn_many(deps, builder)

From outside the DAGPool, you can obtain the results for d and e (or in fact
for any of the build greenthreads) in any of several ways.

:meth:`pool.waitall() <eventlet.dagpool.DAGPool.waitall>` waits until the last of the spawned
greenthreads has completed, and returns a dict containing results for *all* of
them:

::

    final = pool.waitall()
    print("for d: {0}".format(final["d"]))
    print("for e: {0}".format(final["e"]))

waitall() is an alias for :meth:`wait() <eventlet.dagpool.DAGPool.wait>` with no arguments:

::

    final = pool.wait()
    print("for d: {0}".format(final["d"]))
    print("for e: {0}".format(final["e"]))

Or you can specifically wait for only the final programs:

::

    final = pool.wait(["d", "e"])

The returned dict will contain only the specified keys. The keys may be passed
into wait() from any iterable, including a generator.

You can wait for any specified set of greenthreads; they need not be
topologically last:

::

    # returns as soon as both a and zlib have returned results, regardless of
    # what else is still running
    leaves = pool.wait(["a", "zlib"])

Suppose you want to wait specifically for just *one* of the final programs:

::

    final = pool.wait(["d"])
    dprog = final["d"]

The above wait() call will return as soon as greenthread d returns a result --
regardless of whether greenthread e has finished.

:meth:`__getitem()__ <eventlet.dagpool.DAGPool.__getitem__>` is shorthand for
obtaining a single result:

::

    # waits until greenthread d returns its result
    dprog = pool["d"]

In contrast, :meth:`get() <eventlet.dagpool.DAGPool.get>` returns immediately,
whether or not a result is ready:

::

    # returns immediately
    if pool.get("d") is None:
        ...

Of course, your greenthread might not include an explicit return statement and
hence might implicitly return None. You might have to test some other value.

::

    # returns immediately
    if pool.get("d", "notdone") == "notdone":
        ...

Suppose you want to process each of the final programs in some way (upload
it?), but you don't want to have to wait until they've both finished. You
don't have to poll get() calls -- use :meth:`wait_each()
<eventlet.dagpool.DAGPool.wait_each>`:

::

    for key, result in pool.wait_each(["d", "e"]):
        # key will be d or e, in completion order
        # process result...

As with :meth:`wait() <eventlet.dagpool.DAGPool.wait>`, if you omit the
argument to wait_each(), it delivers results for all the greenthreads of which
it's aware:

::

    for key, result in pool.wait_each():
        # key will be a, zlib, b, c, d, e, in whatever order each completes
        # process its result...

Introspection
-------------

Let's say you have set up a :class:`DAGPool <eventlet.dagpool.DAGPool>` with
the dependencies shown above. To your consternation, your :meth:`waitall()
<eventlet.dagpool.DAGPool.waitall>` call does not return! The DAGPool instance
is stuck!

You could change waitall() to :meth:`wait_each()
<eventlet.dagpool.DAGPool.wait_each>`, and print each key as it becomes
available:

::

    for key, result in pool.wait_each():
        print("got result for {0}".format(key))
        # ... process ...

Once the build for a has completed, this produces:

::

    got result for a

and then stops. Hmm!

You can check the number of :meth:`running <eventlet.dagpool.DAGPool.running>`
greenthreads:

::

    >>> print(pool.running())
    4

and the number of :meth:`waiting <eventlet.dagpool.DAGPool.waiting>`
greenthreads:

::

    >>> print(pool.waiting())
    4

It's often more informative to ask *which* greenthreads are :meth:`still
running <eventlet.dagpool.DAGPool.running_keys>`:

::

    >>> print(pool.running_keys())
    ('c', 'b', 'e', 'd')

but in this case, we already know a has completed.

We can ask for all available results:

::

    >>> print(pool.keys())
    ('a',)
    >>> print(pool.items())
    (('a', result_from_a),)

The :meth:`keys() <eventlet.dagpool.DAGPool.keys>` and :meth:`items()
<eventlet.dagpool.DAGPool.items>` methods only return keys and items for
which results are actually available, reflecting the underlying dict.

But what's blocking the works? What are we :meth:`waiting for
<eventlet.dagpool.DAGPool.waiting_for>`?

::

    >>> print(pool.waiting_for("d"))
    set(['c', 'b'])

(waiting_for()'s optional argument is a *single* key.)

That doesn't help much yet...

::

    >>> print(pool.waiting_for("b"))
    set(['zlib'])
    >>> print(pool.waiting_for("zlib"))
    KeyError: 'zlib'

Aha! We forgot to even include the zlib build when we were originally
configuring this DAGPool!

(For non-interactive use, it would be more informative to omit waiting_for()'s
argument. This usage returns a dict indicating, for each greenthread key,
which other keys it's waiting for.)

::

    from pprint import pprint
    pprint(pool.waiting_for())

    {'b': set(['zlib']), 'c': set(['zlib']), 'd': set(['b', 'c']), 'e': set(['c'])}

In this case, a reasonable fix would be to spawn the zlib greenthread:

::

    pool.spawn("zlib", (), builder)

Even if this is the last method call on this DAGPool instance, it should
unblock all the rest of the DAGPool greenthreads.

Posting
-------

If we happen to have zlib build results in hand already, though, we could
instead :meth:`post() <eventlet.dagpool.DAGPool.post>` that result instead of
rebuilding the library:

::

    pool.post("zlib", result_from_zlib)

This, too, should unblock the rest of the DAGPool greenthreads.

Preloading
----------

If rebuilding takes nontrivial realtime, it might be useful to record partial
results, so that in case of interruption you can restart from where you left
off rather than having to rebuild everything prior to that point.

You could iteratively :meth:`post() <eventlet.dagpool.DAGPool.post>` those
prior results into a new DAGPool instance; alternatively you can
:meth:`preload <eventlet.dagpool.DAGPool.__init__>` the :class:`DAGPool
<eventlet.dagpool.DAGPool>` from an existing dict:

::

    pool = DAGPool(dict(a=result_from_a, zlib=result_from_zlib))

Any DAGPool greenthreads that depend on either a or zlib can immediately
consume those results.

It also works to construct DAGPool with an iterable of (key, result) pairs.

Exception Propagation
---------------------

But what if we spawn a zlib build that fails? Suppose the zlib greenthread
terminates with an exception? In that case none of b, c, d or e can proceed!
Nor do we want to wait forever for them.

::

    dprog = pool["d"]
    eventlet.dagpool.PropagateError: PropagateError(d): PropagateError: PropagateError(c): PropagateError: PropagateError(zlib): OriginalError

DAGPool provides a :class:`PropagateError <eventlet.dagpool.PropagateError>`
exception specifically to wrap such failures. If a DAGPool greenthread
terminates with an Exception subclass, the DAGPool wraps that exception in a
PropagateError instance whose *key* attribute is the key of the failing
greenthread and whose *exc* attribute is the exception that terminated it.
This PropagateError is stored as the result from that greenthread.

Attempting to consume the result from a greenthread for which a PropagateError
was stored raises that PropagateError.

::

    pool["zlib"]
    eventlet.dagpool.PropagateError: PropagateError(zlib): OriginalError

Thus, when greenthread c attempts to consume the result from zlib, the
PropagateError for zlib is raised. Unless the builder function for greenthread
c handles that PropagateError exception, that greenthread will itself
terminate. That PropagateError will be wrapped in another PropagateError whose
*key* attribute is c and whose *exc* attribute is the PropagateError for zlib.

Similarly, when greenthread d attempts to consume the result from c, the
PropagateError for c is raised. This in turn is wrapped in a PropagateError
whose *key* is d and whose *exc* is the PropagateError for c.

When someone attempts to consume the result from d, as shown above, the
PropagateError for d is raised.

You can programmatically chase the failure path to determine the original
failure if desired:

::

    orig_err = err
    key = "unknown"
    while isinstance(orig_err, PropagateError):
        key = orig_err.key
        orig_err = orig_err.exc

Scanning for Success / Exceptions
---------------------------------

Exception propagation means that we neither perform useless builds nor wait for
results that will never arrive.

However, it does make it difficult to obtain *partial* results for builds that
*did* succeed.

For that you can call :meth:`wait_each_success()
<eventlet.dagpool.DAGPool.wait_each_success>`:

::

    for key, result in pool.wait_each_success():
        print("{0} succeeded".format(key))
        # ... process result ...

    a succeeded

Another problem is that although five different greenthreads failed in the
example, we only see one chain of failures. You can enumerate the bad news
with :meth:`wait_each_exception() <eventlet.dagpool.DAGPool.wait_each_exception>`:

::

    for key, err in pool.wait_each_exception():
        print("{0} failed with {1}".format(key, err.exc.__class__.__name__))

    c failed with PropagateError
    b failed with PropagateError
    e failed with PropagateError
    d failed with PropagateError
    zlib failed with OriginalError

wait_each_exception() yields each PropagateError wrapper as if it were the
result, rather than raising it as an exception.

Notice that we print :code:`err.exc.__class__.__name__` because
:code:`err.__class__.__name__` is always PropagateError.

Both wait_each_success() and wait_each_exception() can accept an iterable of
keys to report:

::

    for key, result in pool.wait_each_success(["d", "e"]):
        print("{0} succeeded".format(key))

    (no output)

    for key, err in pool.wait_each_exception(["d", "e"]):
        print("{0} failed with {1}".format(key, err.exc.__class__.__name__))

    e failed with PropagateError
    d failed with PropagateError

Both wait_each_success() and wait_each_exception() must wait until the
greenthreads for all specified keys (or all keys) have terminated, one way or
the other, because of course we can't know until then how to categorize each.

Module Contents
===============

.. automodule:: eventlet.dagpool
	:members: