-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
cached_task.py
46 lines (34 loc) · 1.27 KB
/
cached_task.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
"""
This example demonstrates output caching; our first task returns a random number
but requests to be cached for 1.5 minutes. Our second task prints whatever input
it is given.
We run our flow on a 1 minute interval schedule and observe that the output of
`return_random_number` only changes every other run, due to output caching.
"""
import datetime
import random
from prefect import Flow, task
from prefect.schedules import IntervalSchedule
@task(cache_for=datetime.timedelta(minutes=1, seconds=30))
def return_random_number():
return random.random()
@task
def print_number(num):
print("=" * 50)
print("Value: {}".format(num))
print("=" * 50)
schedule = IntervalSchedule(
start_date=datetime.datetime.utcnow(), interval=datetime.timedelta(minutes=1)
)
with Flow("cached-task", schedule=schedule) as flow:
result = print_number(return_random_number)
flow.run()
# ==================================================
# Value: 0.8246312081499598
# ==================================================
# ==================================================
# Value: 0.8246312081499598
# ==================================================
# ==================================================
# Value: 0.36999574748592756
# ==================================================