Generator Based Coroutines · Lecture 12 · 12 min
Pipelines - Pulling Data
Chaining generators builds a lazy pull pipeline, where the consumer at the end requests one item at a time and each stage pulls from the one before it.
A pipeline of generators
A pipeline is a chain of stages where each stage takes data from the previous one, does one small job, and passes the result on. Generators are a natural fit: each stage is a generator that iterates over the previous stage.
In a pull pipeline, the consumer at the end drives everything. When it asks for the next item, the last stage asks the stage before it, and so on back to the source. Nothing happens until somebody asks, and only one item at a time is in flight.
The source: parsing a CSV file
The data is cars.csv, a semicolon-separated file with a header row. The source stage opens the file, detects the dialect, skips the header, and delegates to csv.reader:
import csv
import itertools
def parse_data(f_name):
f = open(f_name)
try:
dialect = csv.Sniffer().sniff(f.read(2000))
f.seek(0)
next(f) # skip the header row
yield from csv.reader(f, dialect=dialect)
finally:
f.close()
for row in itertools.islice(parse_data('cars.csv'), 3):
print(row)yield from csv.reader(...) is already a tiny pipeline: our generator pulls rows from the reader, which pulls lines from the file. The try/finally guarantees the file is closed whether the generator is exhausted or closed early.
A filter stage
A filter keeps only the rows whose car name contains some text:
def filter_data(rows, contains):
for row in rows:
if contains in row[0]:
yield row
data = parse_data('cars.csv')
chevys = filter_data(data, 'Chevrolet')
for row in itertools.islice(chevys, 3):
print(row)The filter doesn’t know or care where its rows come from: a file, a list, another filter. That’s what makes stages composable. Stack a second filter on top of the first:
data = parse_data('cars.csv')
filter_1 = filter_data(data, 'Chevrolet')
filter_2 = filter_data(filter_1, 'Carlo')
for row in filter_2:
print(row)next() ripples back to the file, and one row at a time flows forward.Each time the for loop asks filter_2 for a row, filter_2 keeps asking filter_1 until it gets a row containing 'Carlo'. filter_1 in turn keeps asking parse_data until it gets a Chevrolet. Only one row is in memory at a time, no matter how large the file is.
Seeing the laziness
To prove nothing runs ahead of demand, give the source a counter and stop after the first match:
def parse_data_counting(f_name, counter):
for row in parse_data(f_name):
counter[0] += 1
yield row
counter = [0]
pipeline = filter_data(filter_data(parse_data_counting('cars.csv', counter), 'Chevrolet'), 'Carlo')
print(next(pipeline))
print('rows read from the file so far:', counter[0])
pipeline.close()The first Monte Carlo is the 19th data row of the file, and those 19 rows are all that was read. Closing pipeline closes the generators below it as they’re garbage-collected, and the source’s finally closes the file.
Packaging the pipeline
The caller shouldn’t need to know how the stages are wired. A delegator can build the pipeline and simply yield from its last stage:
def output(f_name, *filter_words):
data = parse_data(f_name)
for word in filter_words:
data = filter_data(data, word) # each filter wraps the previous stage
yield from data
for row in output('cars.csv', 'Chevrolet', 'Carlo', 'Landau'):
print(row)The loop wraps one filter around another, so any number of filter words build a pipeline of any length. The result: just the two Chevrolet Monte Carlo Landau rows.
Add a stage select(rows, *indices) that yields only some columns of each row (say the name and the model year, indices 0 and 7), and plug it in at the end of output. Because every stage takes an iterable and returns one, it slots in anywhere.
Key takeaways
- A pull pipeline is a chain of generators, each iterating over the previous stage. The consumer at the end drives it.
- Stages are lazy: only as much data is read as the consumer asks for, one item at a time.
- Each stage takes any iterable, so stages compose freely. A loop like
data = stage(data, ...)builds pipelines of any length. - A source generator with
try/finally(orwith) releases its resources when the pipeline is exhausted or closed. - A delegator with
yield fromcan hide the wiring of a pipeline behind a single generator.