<@ULVA73B9P> how to schedule a flow run to loop in...
# ask-marvin
c
@Marvin how to schedule a flow run to loop indefnitely without wait. We are using cancel new in queue options with a short 10 second schedule but it floods the flow run history with cancelled flows.
m
thought for 48.5 seconds
Copy code
bash
prefect work-pool create "continuous-pool" --type process --concurrency-limit 1
2. Then, modify your flow deployment to use this work pool and a special "run-once" schedule that will trigger the flow to run continuously:
Copy code
python
from prefect import flow
from prefect.deployments import Deployment
from datetime import timedelta

@flow
def your_flow():
    # your flow logic here
    pass

# Deploy with a run-once schedule
deployment = Deployment.build_from_flow(
    flow=your_flow,
    name="continuous-deployment",
    work_pool_name="continuous-pool",
    schedule={"interval": timedelta(seconds=0)}  # This creates an immediate schedule
)
3. Start a worker for your work pool:
Copy code
bash
prefect worker start -p "continuous-pool"
This approach has several advantages over your current setup: 1. No cancelled runs - the work pool's concurrency limit ensures only one run is active at a time 2. Continuous execution - as soon as one run completes, the next one starts 3. Clean flow run history - you'll only see completed runs, not cancelled ones 4. Better resource management - the worker handles the execution loop instead of relying on the scheduling system If you need to pause the continuous execution, you can either: - Pause the work pool - Stop the worker - Pause the deployment This gives you more control without creating the cleanup issues you're experiencing with the cancelled runs approach. Would you like me to provide more details about any part of this setup, such as how to manage the work pool or deployment programmatically?
c
@Marvin this is essentially what i have so far, was hoping for an inbuilt option in schedules like "run continously without delay" but i cannot set the interval to 0 seconds
m
thought for 117.2 seconds
You're correct - there isn't a built-in "run continuously without delay" option in Prefect's scheduling system, and setting an interval to 0 seconds isn't supported. This is actually by design, as scheduling systems generally need some minimal interval to prevent system overload. Given this limitation, here are a few alternative approaches you could consider: 1. Minimal Interval with Late Run Policy:
Copy code
python
from prefect.deployments import Deployment
from datetime import timedelta

deployment = Deployment(
    name="continuous-deployment",
    flow=your_flow,
    schedule={"interval": timedelta(seconds=1)},  # Minimum practical interval
    work_pool_name="continuous-pool",
    work_queue_name="default",
    work_queue_config={
        "late_runs": {"policy": "run"}  # Ensures runs aren't skipped
    }
)
2. Self-Scheduling Flow:
Copy code
python
from prefect import flow
from prefect.client import get_client
from datetime import datetime, timedelta

@flow
async def continuous_flow():
    # Your flow logic here
    
    # Schedule the next run immediately after completion
    async with get_client() as client:
        await client.create_flow_run_from_deployment(
            deployment_id="your-deployment-id",
            scheduled_time=datetime.now()
        )

# Deploy with no schedule, let the flow handle its own scheduling
deployment = Deployment(
    flow=continuous_flow,
    name="self-scheduling-deployment",
    work_pool_name="continuous-pool"
)
3. Worker-Level Loop:
Copy code
python
from prefect import flow
import asyncio

@flow
async def continuous_flow():
    while True:
        # Your flow logic here
        await asyncio.sleep(0)  # Minimal cooperative yield
Each approach has trade-offs: - Option 1 (Minimal Interval): Cleanest but still has a small delay - Option 2 (Self-Scheduling): More complex but gives you more control - Option 3 (Worker Loop): Simplest but less manageable through Prefect's UI Given your current setup and the issue with cancelled runs, I'd recommend trying Option 1 first with a 1-second interval. It's the most maintainable approach and won't flood your flow run history. The late runs policy ensures that if a run takes longer than the interval, the next run will still execute rather than being cancelled. Would you like me to elaborate on any of these approaches or explore other alternatives?