Inference servers and data loaders group a stream of incoming items into batches: a batch is sent as soon as it is full, or when its oldest item has waited too long. Write that batching logic as a lazy Python generator over a stream of timestamped events.
Implement the generator function micro_batches(events, max_size, timeout). It must use yield.
events: any iterable (often itself a generator) of (t, item) pairs with non-decreasing numeric times t. item is None marks a heartbeat: time passes but nothing arrives.max_size: integer . timeout: number .(flush_time, reason, items) where items is a list in arrival order and reason is one of "size", "timeout", "end".Rules, applied to each event in order:
(deadline, "timeout", items) and start a new empty batch. Then handle the event.None item is appended to the current batch (opening it if empty). If the batch now holds max_size items, yield (t, "size", items) immediately and start a new empty batch.(t_last, "end", items) if the current batch is non-empty, where t_last is the time of the last event read.The generator must be lazy: it reads one event at a time and yields each batch as soon as the rules allow, before reading further. Some tests use sources that fail if read further than necessary.