<@ULVA73B9P> Using Prefect Kubernetes, within a sa...
# ask-marvin
r
@Marvin Using Prefect Kubernetes, within a same flow run, is it possible to run multiple tasks on their respective kubernetes job in parallel? i.e. one task as a kubernetes job
m
thought for 216.2 seconds
Short answer: Not directly. In Prefect 3.x with the Kubernetes worker, a Kubernetes Job is created per flow run, not per task. Tasks inside a single flow run all execute within the same pod/container for that flow run. If you want “one Kubernetes Job per unit of work,” you need to make those units of work flows (subflows) and trigger them as separate deployments. Ways to achieve parallelism: 1) Separate K8s Jobs (recommended): subflows + deployments - Put the work you want isolated into its own
@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:
Copy code
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.
Copy code
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.