Sub topic 15
Beyond DataFrames: a first look at dask.delayed
This chapter assumes you know how lazy evaluation and task graphs work in Dask DataFrames, and that you know the difference between compute() and the schedulers. It also assumes basic Python functions and loops.
Why dask.delayed exists
Dask DataFrames cover work that looks like pandas. Some jobs do not fit that shape. You may have a function that parses one log file, calls a model, or applies custom business rules. You may want to run it on many inputs at once.
dask.delayed turns ordinary Python functions into lazy tasks. The idea is the same as in DataFrames. Calling a delayed function does not run it. It records a node in a task graph. Calling compute() runs the graph on a scheduler.
A first example
Here are three plain functions. Without Dask, the loop runs them one after another.
import dask
from dask import delayed
def load(n):
return list(range(n))
def square_sum(values):
return sum(v * v for v in values)
def combine(results):
return sum(results)
parts = [delayed(load)(n) for n in (10, 100, 1000)]
sums = [delayed(square_sum)(p) for p in parts]
total = delayed(combine)(sums)
print(type(total)) # a Delayed object, nothing has run yet
print(total.compute()) # runs the graph and prints the final numberEach call to a delayed function returns a Delayed object. You can pass a Delayed object as an argument to another delayed function. Dask records that dependency. The three load and square_sum chains do not depend on each other, so the scheduler can run them in parallel.
Using the decorator and computing many results
You can also decorate a function once, so every call is lazy.
import dask
from dask import delayed
@delayed
def double(x):
return 2 * x
tasks = [double(i) for i in range(5)]
results = dask.compute(*tasks) # one call computes all tasks together
print(results) # (0, 2, 4, 6, 8)dask.compute(*tasks) runs all tasks in one pass and returns a tuple. Computing them together lets Dask share any common work. Calling .compute() on each task separately would run several smaller graphs instead.
Common errors
- Calling
compute()inside a delayed function. This hides work from the scheduler and can cause slowdowns or deadlocks. PassDelayedobjects as arguments instead. - Using a
Delayedvalue in aniforfor. Dask does not know the value yet, so it cannot decide the branch or the loop length. Compute first, or move the logic inside a delayed function. - Very small tasks. Each task adds scheduling overhead. If a function takes microseconds, batch many inputs into one call.
- Expecting speedup from threads on pure Python code. The Python GIL (global interpreter lock) lets only one thread run Python bytecode at a time. For CPU-heavy pure Python, use
scheduler="processes"or a distributedClient.
Summary of the choice
| Situation | Better fit |
|---|---|
| Filter and aggregate a large table | Dask DataFrame |
| Run one custom function on many files | `dask.delayed` |
| Apply pandas code to each partition | `map_partitions` |
Exercise
You write results = [delayed(process)(f) for f in files] and then if results[0] > 5:. Why does this fail to behave as expected, and how do you fix it?
Show answer
results[0] is a Delayed object, not a number, because nothing has run yet. Comparing it does not give the value you want. Compute it first, for example value = results[0].compute() and then test value > 5. Or put the comparison inside another delayed function so it also becomes part of the graph.