<@ULVA73B9P> how do I get the name of the worker r...
# marvin-ai
j
@Marvin how do I get the name of the worker running a flow in python?
m
thought for 98.6 seconds
In Prefect 3.x, the simplest way is:
Copy code
python
import os
from prefect import flow

@flow
def my_flow():
    worker_name = os.environ.get("PREFECT__WORKER_NAME")
    print(f"Worker name: {worker_name}")
A few important notes: -
PREFECT__WORKER_NAME
is set by the worker at execution time - it will typically be
None
if the flow is not running under a Prefect worker - there is not a standard
prefect.runtime.flow_run.worker_name
field for this If you also want the worker ID:
Copy code
python
import os
from prefect import flow

@flow
def my_flow():
    print("Worker name:", os.environ.get("PREFECT__WORKER_NAME"))
    print("Worker id:", os.environ.get("PREFECT__WORKER_ID"))
Related docs: - Workers - Runtime context If you want, I can also show how to get this from inside a task or log it automatically for every run.