-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathapp.py
More file actions
60 lines (48 loc) · 1.76 KB
/
app.py
File metadata and controls
60 lines (48 loc) · 1.76 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import restate
from parallelizework.utils import (
Result,
Task,
split,
execute_subtask,
aggregate,
)
# Restate makes it easy to parallelize async work by fanning out tasks.
# Afterward, you can collect the result by fanning in the partial results.
# +------------+
# | Split task |
# +------------+
# |
# ---------------------------------
# | | |
# +--------------+ +--------------+ +--------------+
# | Exec subtask | | Exec subtask | | Exec subtask |
# +--------------+ +--------------+ +--------------+
# | | |
# ---------------------------------
# |
# +------------+
# | Aggregate |
# +------------+
# Durable Execution ensures that the fan-out and fan-in steps happen reliably exactly once.
# Restate makes sure the completions are deterministic on replays.
fan_out_worker = restate.Service("FanOutWorker")
@fan_out_worker.handler()
async def run(ctx: restate.Context, task: Task) -> Result:
# Split the task in subtasks
subtasks = await ctx.run_typed("split task", split, task=task)
# Fan out the subtasks - run them in parallel
result_promises = [
ctx.run_typed(f"execute {subtask}", execute_subtask, subtask=subtask)
for subtask in subtasks.subtasks
]
# Fan in - Aggregate the results
results_done = await restate.gather(*result_promises)
results = [await result for result in results_done]
return aggregate(results)
app = restate.app([fan_out_worker])
if __name__ == "__main__":
import hypercorn
import asyncio
conf = hypercorn.Config()
conf.bind = ["0.0.0.0:9080"]
asyncio.run(hypercorn.asyncio.serve(app, conf))