Providing Dask example with custom DAG, visualization, and spilling
The example sets up a LocalCluster with limited memory to trigger spilling, builds a complex DAG using dask.delayed tasks with cross dependencies, visualizes the execution graph, and shows how to configure memory spilling in the distributed scheduler. It demonstrates analysis including random data generation, transformation, and aggregation steps relying on each other.
Below is an end-to-end example that builds a custom computation graph with dask.delayed, visualizes its DAG, and configures a distributed cluster with limited memory so that workers will spill data to disk once they pass a set threshold.
import numpy as np
import dask
from dask import delayed
from dask.distributed import Client, LocalCluster
# --- 1. Start a small local cluster with a memory limit ---
# Adjust memory_limit downward to more quickly trigger spilling.
cluster = LocalCluster(
n_workers=2,
threads_per_worker=1,
memory_limit="200MB",
dashboard_address=":8787" # visit http://localhost:8787/status to inspect memory/spilling
)
client = Client(cluster)
# Configure memory spill behavior (optional; these are example thresholds)
dask.config.set({
"distributed.worker.memory.target": 0.6, # start spilling at 60% of limit
"distributed.worker.memory.spill": 0.7, # definitely spill by 70%
"distributed.worker.memory.pause": 0.8, # pause worker at 80%
"distributed.worker.memory.terminate": 0.95,
})
# --- 2. Define some functions to create a "complex" DAG ---
def load_block(seed, size):
"""Simulate reading/generating a large block of data"""
rng = np.random.default_rng(seed)
return rng.random(size)
def clean(block):
"""Simulate data cleaning"""
# e.g. clip and normalize
b = np.clip(block, 0.05, 0.95)
return (b - b.mean()) / (b.std() + 1e-6)
def feature_engineering(block_a, block_b):
"""Combine two blocks to create cross-features"""
return block_a * np.sin(block_b) + block_b * np.cos(block_a)
def aggregate(features):
"""Simple aggregation to reduce size"""
return features.mean()
# --- 3. Build a custom DAG with cross-dependencies ---
blocks = [delayed(load_block)(seed=i, size=15_000_000) for i in range(6)]
cleaned = [delayed(clean)(b) for b in blocks]
# Create multiple cross-feature steps to introduce non-trivial edges in the DAG
feats = []
for i in range(len(cleaned) - 1):
feats.append(delayed(feature_engineering)(cleaned[i], cleaned[i + 1]))
# Also combine some non-adjacent blocks to add more edges
feats.append(delayed(feature_engineering)(cleaned[0], cleaned[-1]))
aggregates = [delayed(aggregate)(f) for f in feats]
final_result = delayed(sum)(aggregates) / len(aggregates)
# --- 4. Visualize the execution graph ---
final_result.visualize(filename="custom_dag.svg", rankdir="LR")
print("Graph written to custom_dag.svg")
# --- 5. Execute the computation ---
result = final_result.compute()
print("Final aggregated result:", result)
What this does
- Cluster with limited memory: We create a
LocalCluster with memory_limit="200MB" per worker. Once workers exceed the configured thresholds (target, spill, etc.), they begin spilling partitions to disk automatically. You can watch memory and spill events on the dashboard at http://localhost:8787/status.
- Custom DAG: The code constructs a delayed graph where each “block” is generated, cleaned, and then combined with other blocks. This introduces multiple dependencies between tasks and creates a non-trivial DAG.
- Visualization: Calling
final_result.visualize(...) saves a graph to custom_dag.svg. Opening that file shows the tasks and their dependencies.
- Execution:
compute() sends the graph to the distributed scheduler. If the in-memory footprint grows past the configured thresholds, the scheduler will spill worker data to disk automatically.
Notes and tips
- Spill thresholds: Adjust
distributed.worker.memory.target, spill, and pause to tune when spilling happens. Lowering them makes spilling more aggressive.
- Data size: If you want to ensure spilling, increase the size passed to
load_block or reduce the per-worker memory_limit.
- Monitoring: The Dask dashboard provides live insight into worker memory usage and spill events. Look at the “Workers” and “Task Stream” tabs to see when data spills.
This pattern can be adapted to your real data-loading and processing functions; the key is building the DAG via dask.delayed, visualizing it, and running on a distributed scheduler configured with a memory limit.