Skip to content

Commit 05460cd

Browse files
authored
Fix event handling in BamlStream to use non-blocking queue retrieval (#1596)
<!-- ELLIPSIS_HIDDEN --> > [!IMPORTANT] > Modify `BamlStream.__aiter__` to use non-blocking queue retrieval with `get_nowait()` and handle empty queue with `asyncio.sleep()`. > > - **Behavior**: > - In `BamlStream.__aiter__`, change from blocking `queue.get()` to non-blocking `queue.get_nowait()`. > - Add `asyncio.sleep(0.050)` in `BamlStream.__aiter__` to handle `queue.Empty` exception, allowing for non-blocking event retrieval. > - **Misc**: > - No changes to `BamlSyncStream` or other parts of `stream.py`. > > <sup>This description was created by </sup>[<img alt="Ellipsis" src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=BoundaryML%2Fbaml&utm_source=github&utm_medium=referral)<sup> for ff8b656. It will automatically update as commits are pushed.</sup> <!-- ELLIPSIS_HIDDEN -->
1 parent 0f2c730 commit 05460cd

File tree

1 file changed

+8
-5
lines changed
  • engine/language_client_python/python_src/baml_py

1 file changed

+8
-5
lines changed

engine/language_client_python/python_src/baml_py/stream.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,14 @@ async def __aiter__(self):
7979
try:
8080
self.__drive_to_completion_in_bg()
8181
while True:
82-
event = self.__event_queue.get()
83-
if event is None:
84-
break
85-
if event.is_ok():
86-
yield self.__partial_coerce(event)
82+
try:
83+
event = self.__event_queue.get_nowait()
84+
if event is None:
85+
break
86+
if event.is_ok():
87+
yield self.__partial_coerce(event)
88+
except queue.Empty:
89+
await asyncio.sleep(0.050)
8790
except Exception as e:
8891
raise e
8992
finally:

0 commit comments

Comments
 (0)