Mohsin Ijaz
04/28/2026, 1:12 PMMarvin
04/28/2026, 1:12 PMMarvin
04/28/2026, 1:14 PMwhile True: loop. But Prefect is generally designed around discrete flow runs that start, do work, and then finish in a terminal state.
Recommended approach
- If you want something to run repeatedly, use a schedule or automation trigger
- If you want something polling for work continuously, use:
- flow.serve(...) for a long-lived process that creates flow runs over time
- a worker for deployments in a work pool
So the answer is:
1. Continuous infinite flow run: possible, but not ideal
from prefect import flow
import time
@flow
def my_flow():
while True:
print("doing work")
time.sleep(5)
This works, but has downsides:
- the flow run never reaches Completed
- monitoring/retries are harder
- if the process dies, that single long-lived run is interrupted
- it can look like a stuck/zombie run if not handled carefully
2. Better: run continuously via scheduled flow runs
If you want “always running every N seconds/minutes”, use serve or a deployment schedule.
Example:
from prefect import flow
@flow
def my_flow():
print("doing one unit of work")
if __name__ == "__main__":
my_flow.serve(
name="continuous-ish-flow",
interval=60
)
This means:
- the runner process stays alive
- Prefect creates a new flow run every 60 seconds
- each run finishes cleanly
3. If you need event-driven behavior
Use automations/events instead of an endless loop, depending on your use case.
Helpful docs:
- Serve flows
- Interactive workflows
- Workers
Rule of thumb
- Need a daemon/service? Use serve or an external service that triggers Prefect runs
- Need periodic execution? Use schedules
- Need one flow run to never exit? Possible, but generally not recommended
If you want, I can show the best Prefect pattern for your specific case:
- polling an API continuously
- consuming a queue/stream
- watching a folder
- running a background service