Two Runtimes

Every executor you have sized so far is a single JVM process, and every built-in function Spark offers is JVM code running inside it. A Python function is source code for the Python interpreter, which the JVM cannot execute. So when a stage contains a Python UDF, each executor starts Python worker processes beside itself and hands them rows to compute.

The driver side has its own split. Your program is a Python process that steers a JVM driver through a gateway. Calls like F.upper("product_name") or df.filter(...) only build a plan, and the plan executes entirely inside JVMs. Calling F.udf(fn, "string") works differently: Spark serializes fn, with any variables it closes over, into bytes that travel with each task, because some process on the executor will have to run it.

The driver program builds the plan and the pickled function, the driver JVM ships tasks, and each task pickles its UDF inputs over a local socket to its own Python worker and reads the results back on the same socket.
  • Driver program (source, Python process)
  • Driver JVM (transform, Plans the job, ships tasks)
  • Task sends inputs (transform, Executor JVM); parallelism: 5 tasks
  • Python worker (transform, Separate OS process); parallelism: 5 processes
  • Task reads results (transform, Executor JVM)
  • Rest of the stage (consumer, Executor JVM)
  • Driver program -> Driver JVM
  • Driver JVM -> Task sends inputs
  • Task sends inputs -> Python worker
  • Python worker -> Task reads results
  • Task reads results -> Rest of the stage
  • Driver: Driver program, Driver JVM
  • Executor with 5 cores: Task sends inputs, Python worker, Task reads results, Rest of the stage

Inside a single task

Follow a single task in a stage that computes shout_udf("product_name"), where shout returns its argument in upper case. The task starts in the JVM like any other task and stays there until it needs the UDF's column.

StepProcessWhat happens
1Executor JVMThe task reads its partition and runs any built-in work that comes before the UDF.
2Executor JVMIt asks for a Python worker: a small daemon forks one, or a worker left by an earlier task is reused, since `spark.python.worker.reuse` defaults to true.
3BothThe JVM connects to the worker over a local socket and sends the serialized function.
4Executor JVMInput values are pickled in batches of 100 rows and written to the socket. The full rows wait in a queue inside the JVM.
5Python workerThe worker unpickles each batch, calls your function once per row, pickles the results and writes them back.
6Executor JVMThe task reads the results, converts each to the declared return type, attaches it to its waiting row and continues the stage.

The list contains no shuffle, because the UDF is narrow: each result depends on a single row, so it runs inside the same task as the scan before it and the projection after it, and the stage count stays the same. What changes is how many processes the task needs and how many times each row's values are copied.

Counting the processes

Each running task that evaluates a Python UDF holds its own worker, so the number of Python processes on a node follows the number of task slots. An executor with 5 cores runs 5 tasks at once and keeps 5 Python workers busy next to its JVM. Those workers live outside the JVM heap, so their memory comes from the container's overhead budget, the space whose exhaustion you met as the memoryOverhead OOM. A UDF that builds large Python objects per row can push an executor past its container limit while the heap in the Spark UI looks healthy.

Built-in F.upper
  • Runs as JVM code inside the task
  • Reads Spark's internal row format directly
  • Needs no extra process and no socket
  • Visible to Catalyst, which can optimize around it
Python UDF with the same logic
  • Runs in a Python worker beside the task
  • Converts and pickles each value before the call
  • Makes a socket round trip for every batch
  • Opaque to Catalyst, evaluated in its own plan node

Executors in a cluster have 4 cores each, and worker reuse is on. A stage of 200 tasks applies a single Python UDF. At the busiest moment on one executor, how many Python worker processes are evaluating the UDF?

About this lesson

This section is part of the UDFs and Serialization: Intermediate lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.