A partition generator yields successive chunks from a larger sequence. It prepares data for parallel map operations without constructing every partition at once. section 6.4.3
Use partition size as a performance trade-off:
- Choose chunks that are neither too large nor too small. section 6.4.3
- Avoid tiny chunks because pickling and unpickling each partition can consume the gains from parallelism. section 6.4.3
- Pass each generated chunk to a worker, then partition intermediate results again when parallelizing reduction. section 6.4.3
def partition(data: List,
chunk_size: int) -> List:
for i in range(0, len(data), chunk_size):
yield data[i:i + chunk_size]
Shows the compact generator implementation, which advances through the input by chunk size and yields slices. section 6.4.3
