Concurrent Agents
With the help of asynchronous programming, the concurrent agents can be executed by asyncio.gather in Python.
A simple example is shown below, where two agents are created and executed concurrently.
Agent 1 started at 07:44:41.785Agent 2 started at 07:44:41.785Agent 1 finished at 07:44:44.788Agent 2 finished at 07:44:44.789
import asynciofrom datetime import datetimefrom typing import Anyfrom agentscope.agent import AgentBaseclass ExampleAgent(AgentBase):"""The example agent for concurrent execution."""def __init__(self, name: str) -> None:"""Initialize the agent with its name."""super().__init__()self.name = nameasync def reply(self, *args: Any, **kwargs: Any) -> None:"""Reply to the message."""start_time = datetime.now().strftime("%H:%M:%S.%f")[:-3]print(f"{self.name} started at {start_time}")await asyncio.sleep(3) # Simulate a long-running taskend_time = datetime.now().strftime("%H:%M:%S.%f")[:-3]print(f"{self.name} finished at {end_time}")async def run_concurrent_agents() -> None:"""Run the concurrent agents."""agent1 = ExampleAgent("Agent 1")agent2 = ExampleAgent("Agent 2")await asyncio.gather(agent1(), agent2())asyncio.run(run_concurrent_agents())
Total running time of the script: (0 minutes 3.005 seconds)
Download Jupyter notebook: workflow_concurrent_agents.ipynb
Download Python source code: workflow_concurrent_agents.py
当前内容版权归 AgentScope 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 AgentScope .