|
34 | 34 | ) |
35 | 35 |
|
36 | 36 | import sniffio |
| 37 | +from anyio import create_memory_object_stream, create_task_group, move_on_after |
37 | 38 |
|
38 | | -from ..compat import safe_task |
39 | 39 | from ..exceptions import ApiError, NotFoundError, TransportError |
40 | 40 | from ..helpers.actions import ( |
41 | 41 | _TYPE_BULK_ACTION, |
@@ -99,26 +99,28 @@ async def _chunk_actions( |
99 | 99 | if ret: |
100 | 100 | yield ret |
101 | 101 | else: |
102 | | - item_queue: asyncio.Queue[_TYPE_BULK_ACTION_HEADER_WITH_META_AND_BODY] = ( |
103 | | - asyncio.Queue() |
104 | | - ) |
| 102 | + sender, receiver = create_memory_object_stream[ |
| 103 | + _TYPE_BULK_ACTION_HEADER_WITH_META_AND_BODY |
| 104 | + ]() |
105 | 105 |
|
106 | 106 | async def get_items() -> None: |
107 | 107 | try: |
108 | 108 | async for item in actions: |
109 | | - await item_queue.put(item) |
| 109 | + await sender.send(item) |
110 | 110 | finally: |
111 | | - await item_queue.put((BulkMeta.done, None)) |
| 111 | + await sender.send((BulkMeta.done, None)) |
| 112 | + |
| 113 | + async with create_task_group() as tg: |
| 114 | + tg.start_soon(get_items) |
112 | 115 |
|
113 | | - async with safe_task(get_items()): |
114 | 116 | timeout: Optional[float] = flush_after_seconds |
115 | 117 | while True: |
116 | | - try: |
117 | | - action, data = await asyncio.wait_for( |
118 | | - item_queue.get(), timeout=timeout |
119 | | - ) |
| 118 | + action: _TYPE_BULK_ACTION_WITH_META = {} |
| 119 | + data: _TYPE_BULK_ACTION_BODY = None |
| 120 | + with move_on_after(timeout) as scope: |
| 121 | + action, data = await receiver.receive() |
120 | 122 | timeout = flush_after_seconds |
121 | | - except asyncio.TimeoutError: |
| 123 | + if scope.cancelled_caught: |
122 | 124 | action, data = BulkMeta.flush, None |
123 | 125 | timeout = None |
124 | 126 |
|
|
0 commit comments