<@ULVA73B9P> is there a way for me to add an if st...
# ask-marvin
a
@Marvin is there a way for me to add an if statement into a flow, that says if another flow is running, dont run this one
m
thought for 128.7 seconds
Yes, there are a couple of ways to implement this kind of mutual exclusion between flows in Prefect 3.x. Here's the most straightforward approach using global concurrency limits: 1. First, create a global concurrency limit (GCL):
Copy code
bash
prefect gcl create my-flow-lock --limit 1
2. Then, use it in your flow like this:
Copy code
python
from prefect import flow
from prefect.concurrency import global_concurrency_limit

@flow
def my_flow():
    with global_concurrency_limit("my-flow-lock"):
        # Your flow logic here
        print("Flow is running")
This ensures that only one flow with this concurrency limit can run at a time. If another flow tries to run while the first one is still executing, it will wait until the first one completes. For more complex scenarios, you can also use the concurrency context manager directly:
Copy code
python
from prefect import flow
from prefect.concurrency import ConcurrencyContext

@flow
def my_flow():
    with ConcurrencyContext(key="my-unique-key", max_workers=1):
        # Your flow logic here
        print("Flow is running")
The key difference between these approaches is: - Global concurrency limits (first example) are managed at the server level and persist across restarts - ConcurrencyContext (second example) is more flexible but the limits only exist while the server is running You can find more details about global concurrency limits in the documentation.