-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathfan_out_worker.py
53 lines (42 loc) · 1.77 KB
/
fan_out_worker.py
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
import restate
from restate import Service, Context
from restate.serde import PydanticJsonSerde
from utils import *
# 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.
fanout_worker = Service("FanOutWorker")
@fanout_worker.handler()
async def run(ctx: Context, task: Task) -> Result:
# Split the task in subtasks
subtasks = await ctx.run("split task", lambda: split(task), serde=PydanticJsonSerde(SubTaskList))
# Fan out the subtasks - run them in parallel
result_promises = []
for subtask in subtasks.subtasks:
sub_result_promise = ctx.service_call(run_subtask, arg=subtask)
result_promises.append(sub_result_promise)
# Fan in - Aggregate the results
results = [await promise for promise in result_promises]
return aggregate(results)
# Can also run on FaaS
@fanout_worker.handler()
async def run_subtask(ctx: Context, subtask: SubTask) -> SubTaskResult:
# Processing logic goes here...
# Can be moved to a separate service to scale independently
return await execute_subtask(ctx, subtask)
app = restate.app([fanout_worker])