-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathparallel_async.py
More file actions
37 lines (29 loc) · 1.05 KB
/
Copy pathparallel_async.py
File metadata and controls
37 lines (29 loc) · 1.05 KB
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
"""Generic asynchronous performers."""
from functools import partial
from itertools import count
from ._base import perform
from ._intents import FirstError
def perform_parallel_async(dispatcher, intent, box):
"""
A performer for :obj:`ParallelEffects` which works if all child Effects are
already asynchronous. Use this for things like Twisted, asyncio, etc.
WARNING: If this is used when child Effects have blocking performers, it
will run them in serial, not parallel.
"""
effects = list(intent.effects)
if not effects:
box.succeed([])
return
num_results = count()
results = [None] * len(effects)
def succeed(index, result):
results[index] = result
if next(num_results) + 1 == len(effects):
box.succeed(results)
def fail(index, result):
box.fail(FirstError(exception=result, index=index))
for index, effect in enumerate(effects):
perform(
dispatcher,
effect.on(success=partial(succeed, index), error=partial(fail, index)),
)