Skip to content

Commit 5efc235

Browse files
authored
Long running! (#107)
Signed-off-by: Ketan Umare <kumare3@users.noreply.github.com> Co-authored-by: Ketan Umare <kumare3@users.noreply.github.com>
1 parent ba8fd40 commit 5efc235

2 files changed

Lines changed: 100 additions & 0 deletions

File tree

examples/stress/long_running.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import asyncio
2+
from datetime import timedelta
3+
4+
import flyte
5+
import flyte.report
6+
7+
env = flyte.TaskEnvironment(
8+
name="long_running",
9+
)
10+
11+
12+
@env.task(report=True)
13+
async def long_running_task(duration: timedelta) -> str:
14+
"""
15+
A task that simulates a long-running operation.
16+
It periodically reports a heartbeat to report.log to indicate progress,
17+
every minute.
18+
"""
19+
import time
20+
21+
start_time = time.time()
22+
end_time = start_time + duration.total_seconds()
23+
while time.time() < end_time:
24+
elapsed = time.time() - start_time
25+
await flyte.report.log.aio(f"<p>Elapsed time: {elapsed:.2f} seconds</p>", do_flush=True)
26+
await asyncio.sleep(60)
27+
28+
29+
@env.task(report=True)
30+
async def main_task(duration: timedelta) -> str:
31+
"""
32+
The main task that calls the long-running task.
33+
"""
34+
await flyte.report.log.aio("<h1>Starting long-running task</h1>", do_flush=True)
35+
t = asyncio.create_task(long_running_task(duration=duration))
36+
while not t.done():
37+
await asyncio.sleep(60)
38+
await flyte.report.log.aio("<h1>Long-running task still in progress</h1>", do_flush=True)
39+
return await t
40+
41+
42+
if __name__ == "__main__":
43+
flyte.init_from_config("../../config.yaml")
44+
run = flyte.run(main_task, duration=timedelta(days=5))
45+
print(run.url)
46+
47+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import asyncio
2+
from datetime import timedelta
3+
4+
import flyte
5+
import flyte.report
6+
7+
env = flyte.TaskEnvironment(
8+
name="long_running_reuse",
9+
image=flyte.Image.from_debian_base().with_pip_packages("unionai-reuse"),
10+
reusable=flyte.ReusePolicy(
11+
replicas=1,
12+
concurrency=2,
13+
idle_ttl=timedelta(minutes=5),
14+
)
15+
)
16+
17+
18+
@env.task(report=True)
19+
async def long_running_task(duration: timedelta) -> str:
20+
"""
21+
A task that simulates a long-running operation.
22+
It periodically reports a heartbeat to report.log to indicate progress,
23+
every minute.
24+
"""
25+
import time
26+
27+
start_time = time.time()
28+
end_time = start_time + duration.total_seconds()
29+
while time.time() < end_time:
30+
elapsed = time.time() - start_time
31+
await flyte.report.log.aio(f"<p>Elapsed time: {elapsed:.2f} seconds</p>", do_flush=True)
32+
await asyncio.sleep(60)
33+
34+
35+
@env.task(report=True)
36+
async def main_task(duration: timedelta) -> str:
37+
"""
38+
The main task that calls the long-running task.
39+
"""
40+
await flyte.report.log.aio("<h1>Starting long-running task</h1>", do_flush=True)
41+
t = asyncio.create_task(long_running_task(duration=duration))
42+
while not t.done():
43+
await asyncio.sleep(60)
44+
await flyte.report.log.aio("<h1>Long-running task still in progress</h1>", do_flush=True)
45+
return await t
46+
47+
48+
if __name__ == "__main__":
49+
flyte.init_from_config("../../config.yaml")
50+
run = flyte.run(main_task, duration=timedelta(days=5))
51+
print(run.url)
52+
53+

0 commit comments

Comments
 (0)