-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial.py
More file actions
181 lines (126 loc) · 3.06 KB
/
tutorial.py
File metadata and controls
181 lines (126 loc) · 3.06 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
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
import asyncio
import os
import time
import requests
from prefect import flow, task, unmapped
from pydantic import BaseModel
@task
def call_api(url):
response = requests.get(url)
print(response.status_code)
return response.json()
@task
def parse_fact(response):
fact = response["fact"]
print(fact)
return fact
@flow
def api_flow(url):
fact_json = call_api(url)
fact_text = parse_fact(fact_json)
return fact_text
@flow
def common_flow(config: dict):
print("I am a subgraph that shows up in lots of places!")
intermediate_result = 42
return intermediate_result
@flow
def main_flow():
# do some things
# then call another flow function
data = common_flow(config={})
# do more things
class Model(BaseModel):
a: int
b: float
c: str
@task
def printer(obj):
print(f"Received a {type(obj)} with value {obj}")
print(obj.a, obj.b, obj.c)
@flow(name="My Example Flow",
description="An example flow for a tutorial.",
version=os.getenv("GIT_COMMIT_SHA"))
def model_validator(model: Model):
printer(model)
# note that we define the flow with type hints
@flow
def validation_flow(x: int, y: str):
printer(x)
printer(y)
@task
def task_1():
time.sleep(.005)
pass
@task
def task_2():
print("Task 2")
pass
@flow
def my_flow():
x = task_1()
# task 2 will wait for task_1 to complete
y = task_2(wait_for=[x])
@task
def print_nums(nums):
for n in nums:
print(n)
@task
def square_num(num):
return num ** 2
@flow
def map_flow(nums):
print_nums(nums)
squared_nums = square_num.map(nums)
print_nums(squared_nums)
# MAP
@task
def add_together(x, y):
return x + y
@flow
def sum_it(numbers, static_value):
futures = add_together.map(numbers, static_value)
print(futures)
return futures
@task
def sum_plus(x, static_iterable):
return x + sum(static_iterable)
@flow
def sum_it_unmapped(numbers, static_iterable):
futures = sum_plus.map(numbers, unmapped(static_iterable))
print(futures)
return futures
@task
def my_task_value_error():
raise ValueError()
@flow
def my_flow_error():
try:
my_task_value_error()
except ValueError:
print("Oh no! The task failed.")
state = my_task_value_error(return_state=True)
if state.is_failed():
print("Oh no! The task failed. Falling back to '1'. is_failed")
result = 1
else:
result = state.result()
maybe_result = state.result(raise_on_failure=False)
if isinstance(maybe_result, ValueError):
print("Oh no! The task failed. Falling back to '1'. isinstance")
result += 1
else:
result = maybe_result
return result
@task
async def print_values(values):
for value in values:
await asyncio.sleep(1) # yield
print(value, end=" ")
@flow
async def async_flow():
await print_values([1, 2]) # runs immediately
coros = [print_values("abcd"), print_values("6789")]
# asynchronously gather the tasks
await asyncio.gather(*coros)
asyncio.run(async_flow())