Amir Kamal
07/07/2025, 7:18 AMMarvin
07/07/2025, 7:19 AMMarvin
07/07/2025, 7:21 AMbash
prefect gcl create my-flow-lock --limit 1
2. Then, use it in your flow like this:
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:
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.