Processing Consecutive Batches

Batching groups consecutive inputs under a size limit and must specify the treatment of the final incomplete batch.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
2 min
In this article4 chapters
  1. Trace an uneven result
  2. Reject an unusable stride
  3. Decide whether the final short batch is valid
  4. Clear the right object after publishing a batch

Trace an uneven result

Seven tasks batched at size three form groups of size three, three, and one when all input must be preserved. Dropping the final group loses a task. An exclusive stop index can express each batch as a half-open segment.

Reject an unusable stride

A batch size of zero makes no progress. Validate a positive size before traversal. Test one input smaller than the batch and one divisible by it. Quotient and remainder predicts the number of full batches and the length of the tail.[1]

Decide whether the final short batch is valid

With a capacity of three, the input A B C D E F G yields ABC, DEF, and G if every item is required. A fixed-width matrix reader may reject the last batch. A background worker may send it immediately when a finite source closes, or apply a maximum wait while a source remains open. A capacity of zero is an invalid input because it cannot produce a useful batch. In addition to testing capacity one, test an empty source, eight records at capacity four, and seven records at capacity four. The half-open interval convention keeps those slice boundaries easy to inspect.

Clear the right object after publishing a batch

If a generator yields a mutable list and then calls clear on it, code holding the yielded batches shares the same object. Later inspection can show every batch empty or filled with subsequent records. Give each published batch its own list, or yield an immutable tuple when the consumer expects a snapshot. A batch size of \(b\) controls temporary storage if the consumer keeps up. If that consumer appends all batches to a collection, the combined storage still grows with the total input. Read about shared mutable objects to trace the list identity across a yield.

References

  1. Python Software Foundation, Python Tutorial: Control Flow ↩