@@ -50,41 +50,47 @@ async def append_record_batches(
5050 validate_batching (batching .max_records , batching .max_bytes )
5151 acc = BatchAccumulator (batching )
5252 linger_secs = batching .linger .total_seconds ()
53- aiter = records .__aiter__ ()
53+ record_iter = records .__aiter__ ()
54+ pending_next = None
5455
55- while True :
56- try :
57- record = await anext (aiter )
58- except StopAsyncIteration :
59- break
56+ try :
57+ while True :
58+ if pending_next is not None :
59+ record = await pending_next
60+ pending_next = None
61+ else :
62+ record = await anext (record_iter , None )
63+ if record is None :
64+ break
6065
61- acc .add (record )
62- if acc .is_full ():
63- yield acc .take ()
64- continue
66+ acc .add (record )
6567
66- try :
6768 deadline = (
68- asyncio .get_event_loop ().time () + linger_secs
69+ asyncio .get_running_loop ().time () + linger_secs
6970 if linger_secs > 0
7071 else None
7172 )
7273 while not acc .is_full ():
7374 if deadline is not None :
74- remaining = deadline - asyncio .get_event_loop ().time ()
75+ remaining = deadline - asyncio .get_running_loop ().time ()
7576 if remaining <= 0 :
7677 break
77- record = await asyncio .wait_for (anext (aiter ), timeout = remaining )
78+ next_task = asyncio .create_task (anext (record_iter , None ))
79+ done , _ = await asyncio .wait ({next_task }, timeout = remaining )
80+ if not done :
81+ pending_next = next_task
82+ break
83+ record = next_task .result ()
7884 else :
79- record = await anext (aiter )
85+ record = await anext (record_iter , None )
86+ if record is None :
87+ break
8088 acc .add (record )
81- except StopAsyncIteration :
82- pass
83- except TimeoutError :
84- pass
8589
86- if not acc .is_empty ():
8790 yield acc .take ()
91+ finally :
92+ if pending_next is not None :
93+ pending_next .cancel ()
8894
8995
9096async def append_inputs (
0 commit comments