-
Notifications
You must be signed in to change notification settings - Fork 25
Grapfi 56/improve async in parallel mode, #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,4 @@ | ||||||||||||||
| import asyncio | ||||||||||||||
| from typing import AsyncGenerator | ||||||||||||||
| from typing import List | ||||||||||||||
|
|
||||||||||||||
| from grafi.common.events.topic_events.topic_event import TopicEvent | ||||||||||||||
|
|
@@ -84,8 +83,9 @@ async def _output_listener(self, topic: TopicBase) -> None: | |||||||||||||
| for t in pending: | ||||||||||||||
| t.cancel() | ||||||||||||||
|
|
||||||||||||||
| def __aiter__(self) -> AsyncGenerator[TopicEvent, None]: | ||||||||||||||
| def __aiter__(self) -> "AsyncOutputQueue": | ||||||||||||||
| """Make AsyncOutputQueue async iterable.""" | ||||||||||||||
| self._last_activity_count = 0 | ||||||||||||||
| return self | ||||||||||||||
|
|
||||||||||||||
| async def __anext__(self) -> TopicEvent: | ||||||||||||||
|
|
@@ -114,4 +114,8 @@ async def __anext__(self) -> TopicEvent: | |||||||||||||
| await asyncio.sleep(0) # one event‑loop tick | ||||||||||||||
|
|
||||||||||||||
| if self.tracker.is_idle() and self.queue.empty(): | ||||||||||||||
| raise StopAsyncIteration | ||||||||||||||
| current_activity = self.tracker.get_activity_count() | ||||||||||||||
| # Only terminate if no new activity since last check | ||||||||||||||
| if current_activity == self._last_activity_count: | ||||||||||||||
| raise StopAsyncIteration | ||||||||||||||
| self._last_activity_count = current_activity | ||||||||||||||
|
Comment on lines
+120
to
+121
|
||||||||||||||
| raise StopAsyncIteration | |
| self._last_activity_count = current_activity | |
| raise StopAsyncIteration | |
| # Activity changed while idle; update and briefly pause to avoid busy looping | |
| self._last_activity_count = current_activity | |
| await asyncio.sleep(0.01) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
_last_activity_countattribute is initialized in__aiter__but is not initialized in__init__. This could cause anAttributeErrorif__anext__is called without first calling__aiter__, or if methods try to access this attribute before iteration starts. Consider initializing this attribute in__init__to ensure it always exists.