Sub topic 9
Using a Client and the dashboard
This chapter assumes you know the basics of Dask DataFrames, lazy evaluation, and compute(). It also assumes you have read the previous chapter on choosing a local scheduler. You will start a Client, open the dashboard, and use it to watch a computation run.
What a Client does
A Client connects your Python session to a Dask cluster. Calling Client() with no arguments starts a local cluster on your machine. It creates a scheduler process and several worker processes. It also registers itself as the default scheduler. After that, every compute() and persist() call in the session runs on that cluster, with no scheduler= argument needed.
The distributed scheduler is also a good choice on a single machine. It gives you the dashboard, better memory management, and clearer error messages than the plain threaded and process schedulers.
import pandas as pd
import dask.dataframe as dd
from dask.distributed import Client
if __name__ == "__main__":
client = Client() # starts a local cluster
print(client.dashboard_link)
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())
client.close()Reading the dashboard
client.dashboard_link prints a URL, usually http://127.0.0.1:8787/status. Open it in a browser while a computation runs. The dataset in the example above is tiny, so the work finishes too fast to watch. Use a larger dataset to see the dashboard in action.
The most useful pages are:
| Page | What it shows | What to look for |
|---|---|---|
| Status | Task stream, progress bars, memory per worker | Gaps in the task stream mean workers are idle |
| Workers | CPU and memory per worker | One worker with much more memory than the others |
| Graph | The task graph as it executes | Many tasks waiting on one task |
| Profile | Where time goes inside tasks | Slow functions in your own code |
Memory bars change color as a worker fills up. Orange means the worker is near its limit and has started writing data to disk. Red means the worker is paused or at risk of being killed.
How work flows through the cluster
The scheduler tracks which tasks are ready, running, or done. The dashboard is a view of that state, which is why it updates live.
Configuring the local cluster
Client() picks a worker count and memory limit from your machine. You can set them directly with LocalCluster.
from dask.distributed import Client, LocalCluster
if __name__ == "__main__":
cluster = LocalCluster(n_workers=2, threads_per_worker=2, memory_limit="2GB")
client = Client(cluster)
print(client)
client.close()
cluster.close()Each worker gets its own memory_limit. Two workers with "2GB" use up to 4 GB in total. As a rule of thumb, pandas-heavy code holds the GIL (Python's global interpreter lock), so it benefits from more workers with fewer threads each. Code that calls NumPy or other libraries that release the GIL does well with more threads.
Using persist with a Client
On a cluster, persist() keeps partitions in the workers' memory. It returns right away, and the work continues in the background. You can watch the memory bars fill on the Status page.
ddf = ddf.persist() # returns immediately, work runs on the clusterLater computations reuse the persisted partitions instead of rereading the source.
Exercise
You run compute() on a large DataFrame with a Client active, and the Status page shows long gaps in the task stream with workers mostly idle. Name one possible cause and one dashboard page you would check next.
Show answer
One cause is a bottleneck such as a task that many others wait on, for example a shuffle or a single large partition, or too few partitions to keep all workers busy. Check the Graph page to see which tasks are blocking others, or the Workers page to see whether one worker holds most of the data.