Skip to content

Build and run agents you can see, understand and trust.

fix(agent): add `asyncio.sleep(0)` in `print` method to avoid blocking the consumption in `stream_printing_messages` (#1218)

qbcb9851f6c8567
diff --git a/src/agentscope/agent/_agent_base.py b/src/agentscope/agent/_agent_base.py
index e2023cd5d..7a49b2702 100644
--- a/src/agentscope/agent/_agent_base.py
+++ b/src/agentscope/agent/_agent_base.py
@@ -222,6 +222,10 @@ class AgentBase(StateModule, metaclass=_AgentMeta):
"""
if not self._disable_msg_queue:
await self.msg_queue.put((deepcopy(msg), last, speech))
+ # Yield control to the event loop, allowing consumer coroutines
+ # to process messages from the queue. This prevents the producer
+ # from monopolizing the event loop.
+ await asyncio.sleep(0)
if self._disable_console_output:
return