Skip to content

这个mcp两个client同时连接第2个会阻塞 #232

@gauss-clb

Description

@gauss-clb

大致可以参考下面代码,这里的Agent调用了qwen-agent,agent_load = Agent(enable_thinking=True, verbose=True)这行代码是为了加载MCPManager,因为qwen-agent这个框架里这个类是单例的,直接并发加载会报错,先提前加载一下。

import asyncio
from agent_cls import Agent

agent_load = Agent(enable_thinking=True, verbose=True)

def call():
    import time
    st = time.time()
    agent = Agent(enable_thinking=True, verbose=True)
    agent.set_system_message('hello')
    ed = time.time()
    print(f'Cost: {ed-st}', flush=True)


async def init_agent(name):
    print(f"[{name}] init ...")
    await asyncio.to_thread(call)

async def main():
    # 并发启动两个 agent
    print("Starting two agents in parallel...\n")

    await asyncio.gather(
        init_agent("Agent-1"),
        init_agent("Agent-2"),
    )

if __name__ == "__main__":
    asyncio.run(main())
Image 这个是qwen-agent加载mcp的代码部分,箭头指的部分只会打印1次,另1个就卡在这里,等300s超时。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions