<@ULVA73B9P> In this example, let's say i set each...
# ask-marvin
s
@Marvin In this example, let's say i set each flow to run every 2 seconds, how do i prevent parallel runs of a flow before it finishes, specifically slow() without blocking hello_world()
Copy code
from prefect import flow, task, serve
import time

@flow(log_prints=True)
def hello_world(name: str = "world", goodbye: bool = False):
    x = 1
    s = str(x)
    print(f"Hello {name} {s} from Prefect! :hugging_face:")
    x += 1

    if goodbye:
        print(f"Goodbye {name}!")

@flow(log_prints=True)
def slow():
    time.sleep(100)
    print("ok")

if __name__ == "__main__":
    a_deploy = hello_world.to_deployment(name="hellow")
    b_deploy = slow.to_deployment(name="slow")
    serve(a_deploy, b_deploy)
m
To prevent parallel runs of a flow (such as your
slow
flow) while allowing other flows (like
hello_world
) to execute independently, you can use global concurrency limits in Prefect 2.x. Here's an excerpt from the documentation that describes how to set up concurrency limits:
Copy code
python
from prefect import flow, concurrency

@flow
def my_flow():
    with concurrency("database", occupy=1):
        # your flow code here
When using
concurrency
within Prefect, you can define a key (in this case, "database") and set the
occupy
parameter to 1. This ensures that only one instance of the flow using this concurrency key runs at a time. For your scenario, you need to wrap your
slow
flow's critical section within a
concurrency
block, that could look something like this:
Copy code
python
from prefect import flow, concurrency
import time

@flow(log_prints=True)
def slow():
    with concurrency("slow_concurrency_key", occupy=1):
        time.sleep(100)
        print("ok")
By doing this, the
slow
flow will not run in parallel with itself, but it will not block the
hello_world
flow. Unfortunately, no related GitHub issues were found, so there might not be many reports or questions in the community about similar scenarios - which either suggests it's straightforward to use or not commonly hit upon as an issue. Remember to refer to Prefect's official documentation for global concurrency limits for more in-depth information. Note: In the documentation, they used the key
"database"
, however, you should replace it with a key that accurately reflects the resource or process you're trying to limit, such as
"slow_concurrency_key"
in this example. The provided example was unrelated to concurrency limits, so I focused on giving you a tailored response.