To programmatically distinguish whether a flow is being run locally or on a work pool, you can utilize Prefect's
runtime context, which tracks information about the current flow or task run. It contains various internal objects used by Prefect and is accessible only during a run. While the runtime context does not directly provide the name of the work pool being used, you could potentially set environment variables or use metadata within your flow or deployment to track this information.
For instance, you might define environment variables that indicate the execution environment or work pool when setting up your worker. By accessing the runtime context within your flow, you can make decisions based on these variables. However, the documentation does not provide a direct means of detecting the work pool from within the running flow.
You can read more about the runtime context in Prefect's
Runtime Context documentation and learn about work pools in
Work Pools documentation.
Unfortunately, there doesn't seem to be a straightforward method provided out of the box for distinguishing runtime environments, but with custom setup and environment variables, it can be achieved. Be mindful that this might involve some additional effort to set up and maintain this detection logic.