Sub topic 8
Choosing a scheduler
A Dask DataFrame operation builds a task graph. A scheduler is the component that runs that graph. This chapter covers the three local schedulers and how to pick one. It assumes you know what a task graph is and how compute() triggers it, and that you can write basic pandas code.
What a scheduler does
When you call compute(), the scheduler looks at the graph and decides which tasks can run now, which must wait for their inputs, and where each one runs. The graph stays the same. Only the way it is executed changes.
Dask DataFrames use the threaded scheduler by default. You can override it for one call with the scheduler= argument.
import pandas as pd
import dask.dataframe as dd
pdf = pd.DataFrame({
"zone": ["a", "b", "a", "b"],
"fare": [10.0, 20.0, 30.0, 40.0],
})
ddf = dd.from_pandas(pdf, npartitions=2)
mean_fare = ddf[ddf["fare"] > 15].groupby("zone")["fare"].mean()
print(mean_fare.compute(scheduler="threads"))
print(mean_fare.compute(scheduler="processes"))
print(mean_fare.compute(scheduler="synchronous")) # single thread, easy to debugAll three calls return the same pandas Series. Only the execution differs.
The three local schedulers
| Scheduler | How it runs tasks | Best for | Main cost |
|---|---|---|---|
| `threads` | A pool of threads in one process | Numeric pandas and NumPy work | Python code holding the GIL does not run in parallel |
| `processes` | A pool of separate Python processes | Pure-Python work, such as string handling or custom loops | Data is copied between processes |
| `synchronous` | One thread, one task at a time | Debugging and profiling | No parallelism |
Threads
The GIL (global interpreter lock) allows only one thread to run Python bytecode at a time. Many pandas and NumPy operations release the GIL while they work in compiled code, so threads can run them in parallel. Threads share memory, so nothing is copied. This is why threads is the default for DataFrames.
Processes
Each process has its own interpreter and its own GIL. Work that is mostly Python code can therefore use several cores. The price is serialization. Inputs and results must be pickled and sent between processes. If a task returns a large object, that transfer can erase the gain.
Synchronous
The synchronous scheduler runs every task in the calling thread. A normal traceback appears at the line that failed. You can also use pdb or an IDE debugger, which do not work well across threads or processes.
import dask
dask.config.set(scheduler="synchronous")This sets the scheduler for the rest of the session. To limit it to a block of code, use with dask.config.set(scheduler="synchronous"):.
Choosing in practice
Use these rules:
- Start with the default
threads. It suits typical pandas aggregations. - Switch to
synchronouswhen a task fails and you need a clear traceback. - Try
processesif CPU use stays near one core and your functions are pure Python. - Move to the distributed Client when you want the dashboard or more than one machine. The next chapter covers it.
Common errors
- Using
processeson Windows or macOS without a main guard. These systems start new processes by importing your script again. Put the code that starts work underif __name__ == "__main__":, or the script can fail or launch processes repeatedly. - Expecting
processesto speed up everything. Moving large partitions between processes can cost more than the computation. - Debugging with
threads. Errors still appear, but the traceback is harder to follow and debuggers behave unpredictably. Reproduce the problem withsynchronousfirst. - Forgetting that a Client overrides the default. After you create a
Client,compute()uses it unless you passscheduler=explicitly.
Exercise
A map_partitions function raises a confusing error under the default scheduler. Which scheduler do you switch to, and why? Then, if the function is slow pure-Python string processing, which scheduler might speed it up?
Show answer
Switch to synchronous. It runs tasks one at a time in your own thread, so you get a normal traceback and can use a debugger. For slow pure-Python string work, try processes. Each process has its own GIL, so tasks can use several cores, as long as the cost of copying data between processes stays small.
Exercise
Why is threads a good default for Dask DataFrames even though Python has a GIL?
Show answer
Most of the time in pandas and NumPy operations is spent in compiled code that releases the GIL, so threads run in parallel. Threads also share memory, so partitions are not copied.