Romain Vincent
12/03/2025, 2:33 PMMarvin
12/03/2025, 2:33 PMMarvin
12/03/2025, 2:37 PM@flow.
- Create a deployment for that flow on a Kubernetes work pool.
- From your orchestrator flow, trigger many runs of that deployment in parallel using run_deployment. Each call becomes its own K8s Job.
Example:
from prefect import flow
from prefect.deployments import run_deployment
@flow
def orchestrate_batches(batches: list[list[int]]):
# Fire off one K8s Job per batch
child_runs = []
for i, batch in enumerate(batches):
fr = run_deployment(
name="process-batch/k8s", # "<flow_name>/<deployment_name>"
parameters={"batch": batch},
timeout=0, # do not block; submit all quickly
# optional: per-run K8s overrides
job_variables={"pod": {"resources": {"limits": {"cpu": "2", "memory": "2Gi"}}}},
)
child_runs.append(fr)
return [fr.id for fr in child_runs]
- timeout=0 submits and returns immediately so you can kick off many in parallel.
- If you instead want to wait for each to finish before continuing, omit timeout (or set a positive timeout) in a loop (note: that will block per call).
- API reference: run_deployment
2) Parallelism within the same K8s Job (same pod): task runners
- If isolation is not required and you just want parallel execution, use a task runner.
from prefect import flow, task
from prefect.task_runners import ThreadPoolTaskRunner
@task
def work(x):
return x * 2
@flow(task_runner=ThreadPoolTaskRunner(max_workers=8))
def parallel_in_one_pod(items: list[int]):
futures = [work.submit(i) for i in items]
return [f.result() for f in futures]
- This runs tasks in threads or processes inside the single pod for the flow run.
3) Advanced: task workers (distributed tasks)
- If you want tasks to execute outside the main flow process but not necessarily as standalone K8s Jobs, you can use Prefect task workers with PrefectTaskRunner. This adds infra but gives distributed task execution. Happy to share a template if that’s the direction you want.
Key takeaway:
- Per-task K8s job = not supported.
- Per-flow-run K8s job = supported. Use subflows + deployments + run_deployment.
- For simple parallelism inside one pod, use ThreadPoolTaskRunner or ProcessPoolTaskRunner.
Follow-up: Do you specifically need one Kubernetes Job per unit of work, or is parallelism within a single pod sufficient? If you need per-job isolation, I can help sketch the subflow/deployment layout and K8s work pool configuration.