02 / MAKE EXECUTION DEPENDABLE · LESSON 05

Stream work in bounded batches

Use iterators to limit how much work is consumed before the caller is ready.

15 min · learn at your pace

A generator controls consumption

Turning every input into a list is convenient until that input becomes large or expensive. An iterator lets a caller request work incrementally. This lesson groups an iterable into small lists without consuming the next batch until the caller requests it. The source can itself be a generator with visible side effects.

islice reads at most the requested number of items. An empty slice means the source is exhausted. Yield a final short batch rather than discarding it: losing the last few endpoints would make an assessment incomplete. Preserve order and preserve duplicates here; deduplication belongs to the earlier scan-planning boundary.

Validate before returning the iterator

The body of a generator function does not run when the function is called. It starts on the first next call. If callers need invalid batch sizes rejected immediately, validate in an ordinary outer function and return an inner generator. This distinction matters when validation must happen before a job is accepted.

The batch limit bounds memory and creates convenient cancellation checkpoints. It does not create parallelism. The browser's current script runner executes synchronously; launching asyncio tasks without awaiting them would let an exercise finish before its work completes. This course keeps scheduling explicit and deterministic rather than claiming that a list of batches is concurrent execution.

Later tools can process each batch, persist completed results and check for cancellation before consuming another one. The same small iterator contract remains useful when a local application eventually supplies a different execution mechanism.

Try an example.py
from itertools import islice
source = iter(range(7))
print(list(islice(source, 3)))
print(list(islice(source, 3)))
YOUR TURN

Put it into practice.

Define batches(items, size). Validate size immediately as integer 1..64 excluding bool, raising ValueError before returning. Return an iterator of nonempty lists, each with at most size items, preserving order and duplicates. Consume no items until next is requested, and never consume ahead of the returned batch. Keep a final short batch; empty input yields no batches.

See an explained solution
One way to solve it.py
from itertools import islice

def batches(items, size):
    if type(size) is not int or not 1 <= size <= 64:
        raise ValueError("Batch size must be 1..64")
    iterator=iter(items)
    def generate():
        while True:
            batch=list(islice(iterator,size))
            if not batch:return
            yield batch
    return generate()

if __name__ == "__main__":
    print(list(batches(range(7), 3)))

The generator limits consumption rather than promising parallelism. Immediate validation and lazy work make cancellation and checkpoint boundaries predictable.