Skip to content

Commit 0c38e9a

Browse files
committed
feat: add retrigger param to control
1 parent 5f39472 commit 0c38e9a

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

examples/5_retrigger_type.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import asyncio
2+
from drive_flow import default_drive, EventInput
3+
from drive_flow.dynamic import goto_events
4+
5+
6+
@default_drive.make_event
7+
async def on_start(event: EventInput, global_ctx):
8+
print("---------New_turn---------")
9+
10+
11+
@default_drive.listen_group([on_start])
12+
async def a(event: EventInput, global_ctx):
13+
await asyncio.sleep(0.1)
14+
print("a")
15+
16+
17+
@default_drive.listen_group([on_start])
18+
async def b(event: EventInput, global_ctx):
19+
await asyncio.sleep(0.5)
20+
print("b")
21+
22+
23+
@default_drive.listen_group([a, b], retrigger_type="any")
24+
async def c(event: EventInput, global_ctx):
25+
print("C is triggered")
26+
27+
28+
# default retrigger_type is 'all'
29+
@default_drive.listen_group([a, b], retrigger_type="all")
30+
async def d(event: EventInput, global_ctx):
31+
print("D is triggered")
32+
return goto_events([on_start]) # re-loop the workflow
33+
34+
35+
if __name__ == "__main__":
36+
asyncio.run(default_drive.invoke_event(on_start))
37+
38+
# For the first turn, the print will be:
39+
# ---------New_turn---------
40+
# a
41+
# b
42+
# C is triggered
43+
# D is triggered
44+
45+
# But for the rest of the turns, the print will be:
46+
# ---------New_turn---------
47+
# a
48+
# C is triggered
49+
# b
50+
# C is triggered
51+
# D is triggered
52+
53+
# Because the retrigger_type of d is 'all', it will be triggered only when all the events in the group (a, b) are updated.
54+
# The retrigger_type of c is 'any'. So when a is updated, it will trigger c, and when b is updated, it will trigger c again.
File renamed without changes.

readme.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ In this example, The return of `hello` event will trigger `world` event.
6969

7070
To make an event function, there are few elements:
7171

72-
* Input Signature: must be `(event: EventInput, global_ctx)`
73-
* `EventInput` is the returns of the listening groups.
74-
* `global_ctx` is set by you when invoking events, it can be anything and default to `None`
72+
* Input Signature: must be `(event: EventInput, global_ctx)`. `EventInput` is the returns of the listening groups. `global_ctx` is set by you when invoking events, it can be anything and default to `None`
7573
* Make sure you decorate the function with `@default_drive.make_event` or `@default_drive.listen_group([EVENT,...])`
7674

7775
Then, run your workflow from any event:
@@ -80,7 +78,7 @@ Then, run your workflow from any event:
8078
await default_drive.invoke_event(EVENT, EVENT_INPUT, GLOBAL_CTX)
8179
```
8280

83-
Check out [examples](./examples) for more user cases!
81+
Check out [examples](./examples) for more use cases and features!
8482

8583
## Features
8684

@@ -118,7 +116,12 @@ assert results[adding.id] == 3
118116
```
119117
</details>
120118

119+
`drive_flow` suppports different behaviors for multi-event triggering:
121120

121+
- `all`: retrigger this event only when all the listening events are updated.
122+
- `any`: retrigger this event as long as one of the listening events is updated.
123+
124+
Check out this [example](./examples/5_retrigger_type.py) for more details
122125

123126
### Parallel
124127

@@ -158,15 +161,18 @@ asyncio.run(default_drive.invoke_event(start))
158161
`drive_flow` is dynamic. You can use `goto` and `abort` to change the workflow at runtime:
159162

160163
<details>
161-
<summary> code snippet for abort</summary>
164+
<summary> code snippet for abort_this</summary>
162165

163166
```python
164167
from drive_flow.dynamic import abort_this
165168

166169
@default_drive.make_event
167170
async def a(event: EventInput, global_ctx):
168171
return abort_this()
169-
172+
# abort_this is not exiting the whole workflow,
173+
# only abort this event's return and not causing any other influence
174+
# `a` chooses to abort its return. So no more events in this invoking.
175+
# this invoking then will end
170176
@default_drive.listen_group([a])
171177
async def b(event: EventInput, global_ctx):
172178
assert False, "should not be called"

0 commit comments

Comments
 (0)