liveBook · Manning

Take

As explained in Python Concurrency with asyncio

take is a book-local asynchronous generator that yields a requested maximum number of items from another asynchronous generator.

take is a reusable asynchronous generator for limiting another asynchronous generator to a requested maximum number of items. It supports fixed-size retrieval while preserving the convenience of an async for loop and avoiding a complete result set in memory. section 5.7.2

Taking the first five products

Wrap a database cursor with take(product_generator, 5), then iterate normally with async for. The generator counts yielded records and stops after the fifth product. section 5.7.2

How Python Concurrency with asyncio covers it

How deep it goes
Defines the counter-based generator and demonstrates its use, while deferring broader asynchronous-stream processing to aiostream.
Examples
Demonstrates taking five product records from an asyncpg database cursor and printing the resulting records.
What it assumes
Assumes asynchronous iteration and database cursors, while showing how to replace an ad hoc counter-and-break loop.
What you can do after
Apply it by wrapping an asynchronous generator when fixed-size streaming retrieval is needed.
How it connects
Connects asynchronous generators with streaming cursors, async for loops, prefetching, and asynchronous stream processing.

This explainer was generated by Manning's knowledge graph from the text of Python Concurrency with asyncio. The section links point to the parts of the book it draws on.