<@ULVA73B9P> What is the best way of programmatica...
# ask-marvin
j
@Marvin What is the best way of programmatically getting a list of all currently running flow-runs?
m
You can get a list of all currently running flow-runs using the Prefect CLI with the following command:
Copy code
bash
prefect flow-run ls
This command will list all flow-runs. If you want to filter only the running ones, you might need to add some additional filtering. Please note that this command lists all flow-runs, not just the currently running ones. If you want to filter only the running ones, you might need to add some additional filtering or use the Prefect API directly. For more detailed information, you might want to check the Prefect documentation or their community forum.
j
@Marvin how would I use the Prefect API to get a list of only currently running flow-runs?
m
To get a list of currently running flow-runs using the Prefect API, you would need to use the
flow_run
query with the
state
filter set to "Running". Here is an example of how you might do this:
Copy code
from prefect import Client

# Instantiate the client
client = Client()

# Query for running flow runs
flow_runs = client.graphql(
    """
    query {
        flow_run(where: { state: { _eq: "Running" } }) {
            id
            name
            state
        }
    }
    """
)

# Print the running flow runs
for flow_run in flow_runs.data.flow_run:
    print(f"ID: {flow_run.id}, Name: {flow_run.name}, State: {flow_run.state}")
This script will print the ID, name, and state of each running flow run. Please note that you will need to have the Prefect server running and be authenticated with the Prefect API for this script to work. For more information on how to do this, please refer to the Prefect documentation.
n
gosh no @Marvin graphql is so prefect 1, look for
read_flow_runs
on the orchestration client
👀 1
m
To get a list of currently running flow-runs using the Prefect API, you can use the
read_flow_runs
method of the
Client
class in the
prefect
module. Here is an example:
Copy code
from prefect import Client

# Instantiate the client
client = Client()

# Get all flow runs
all_flow_runs = client.read_flow_runs()

# Filter for running flow runs
running_flow_runs = [fr for fr in all_flow_runs if fr.state == 'Running']

# Print the running flow runs
for flow_run in running_flow_runs:
    print(f"ID: {flow_run.id}, Name: {flow_run.name}, State: {flow_run.state}")
This script will print the ID, name, and state of each running flow run. Please note that you will need to have the Prefect server running and be authenticated with the Prefect API for this script to work. For more information on how to do this, please refer to the Prefect documentation.
👎 1
j
is getting a list of running flows-runs from the client before the best way to ensure only a single flow-run with a given name is running? Or is there a slicker way to do this?
n
is it a deployment or just a flow?
you can use concurrency limits either way i believe, but exactly how will depend on which it is
j
I want to be able to run many flows from the same deployment simultaneouly, but if I launch
instrument-A-flow-run
on Tuesday, I don't want it to launch on Wednesday if the previous
instrument-A-flow-run
is still working
so there may be a way to set concurrency to 1 for a flow-run name?
n
im pretty sure there's a way we can do this with events - let me get back to you on that when im back online
j
# ask-marvin-then-nate
🤣 1