Jackson Nguyen
12/03/2025, 1:27 AMMarvin
12/03/2025, 1:27 AMMarvin
12/03/2025, 1:36 AMprefect deployment run or used the client)?
Here’s how it works in Prefect 3.x:
What you can get from inside a running flow
- Work pool and queue: available on the current FlowRun
- Worker name: recorded as a label on the FlowRun
- Hostname of the executing machine: use standard Python (not a Prefect field)
- Who triggered the run: available via the FlowRun’s created_by (user or token), but Prefect does not record the submitter’s machine/hostname
Example (run inside your flow):
from prefect import flow
from prefect.runtime import flow_run
from prefect.context import FlowRunContext
import socket
@flow
def inspect_runtime():
ctx = FlowRunContext.get()
fr = ctx.flow_run if ctx else None
# Basic flow run info
print("Flow run ID:", flow_run.id)
print("Flow run name:", flow_run.name)
if fr:
# Worker / pool / queue
print("Work pool:", fr.work_pool_name)
print("Work queue:", fr.work_queue_name)
# Worker name recorded by the worker as a label
worker_name = (fr.labels or {}).get("<http://prefect.io/worker-name|prefect.io/worker-name>")
print("Worker name:", worker_name)
# Who created this run (user/token), not their machine
creator = getattr(fr.created_by, "display_value", None) if fr.created_by else None
print("Created by:", creator)
# Infrastructure/job details if set
print("Job variables:", fr.job_variables)
# Actual executing machine identity (OS-level)
print("Hostname:", socket.gethostname())
Key points
- Executing machine: You can infer the worker via labels and get the OS hostname with Python. Prefect does not expose the executing host’s name directly as a Prefect field.
- Submitting/triggering machine: Prefect records who created the run (created_by) but does not record the submitter’s machine/hostname.
Helpful docs
- Runtime context overview: Runtime context concepts
- Access runtime info: How to access runtime info
- FlowRun model (fields like work_pool_name, labels, job_variables, created_by): FlowRun API reference
- Context API (FlowRunContext/TaskRunContext): Context API
- Work pools and workers concepts: Work pools and Workers
If you can confirm which you need (executing machine vs. submitting machine), I can tailor a snippet to your setup (e.g., Process, Docker, or Kubernetes).Jackson Nguyen
12/03/2025, 1:39 AMprefect deployment run or used the client)Marvin
12/03/2025, 1:39 AMMarvin
12/03/2025, 1:43 AMcreated_by (user or token).
- If you need submitter machine info, pass it at submission time (as a parameter, tag, or job variable) and read it during the run.
What you can get today (who triggered it)
Inside your flow:
from prefect.context import FlowRunContext
ctx = FlowRunContext.get()
creator = getattr(ctx.flow_run.created_by, "display_value", None) if ctx and ctx.flow_run.created_by else None
print("Created by:", creator) # e.g., a user or service token
How to capture the submitter machine going forward
Option A — CLI
- Pass it as a parameter or job variable when you run the deployment.
- Values are parsed as JSON, so strings must be quoted.
Examples:
# Parameter (flow must accept the parameter, e.g., submitter_host: str | None = None)
prefect deployment run <flow>/<deployment> --param submitter_host='"'"$(hostname)"'"'
# Job variable (available at runtime via runtime.flow_run.job_variables)
prefect deployment run <flow>/<deployment> --job-variable submitter_host='"'"$(hostname)"'"'
Read it in your flow:
from prefect import flow
from prefect.runtime import flow_run
@flow
def my_flow(submitter_host: str | None = None):
host_from_param = submitter_host
host_from_job_vars = (flow_run.job_variables or {}).get("submitter_host")
print("Submitter host (param):", host_from_param)
print("Submitter host (job var):", host_from_job_vars)
Option B — Python client
Attach submitter info when creating the run.
import socket, datetime, asyncio
from prefect import get_client
async def trigger(deployment_id):
submitter = {
"host": socket.gethostname(),
"time": datetime.datetime.utcnow().isoformat()
}
async with get_client() as client:
await client.create_flow_run_from_deployment(
deployment_id=deployment_id,
parameters={"submitter": submitter}, # or {"submitter_host": submitter["host"]}
# tags=["submitted-from:"+submitter["host"]] # optional if you want to filter by it
)
Read it in your flow:
from prefect.runtime import flow_run
submitter = (flow_run.parameters or {}).get("submitter")
Notes and links
- This is by design: Prefect records the creator (user/token) but not the creator’s machine.
- Runtime context overview: Runtime context concepts
- Access runtime info in flows: How to access runtime info
- FlowRun model (includes created_by, job_variables, work_pool_name, etc.): FlowRun API reference
- Context API (FlowRunContext): Context API
If you tell me how you trigger runs (CLI vs. Python, and your shell/OS), I can give you an exact one-liner or snippet with correct quoting.