In the previous example, we created a simple generator that reads rows from a database, and when we wished to finish its iteration, this generator released the resources linked to the database. This is a good example of using one of the methods that generators provide (close), but there is more we can do.
An obvious of such a generator is that it was reading a fixed number of rows from the database.
We would like to parametrize that number (10) so that we can change it throughout different calls. Unfortunately, the next() function does not provide us with options for that. But luckily, we have send():
def stream_db_records(db_handler): retrieved_data = None previous_page_size = 10 try: while True: page_size = yield retrieved_data ...