chatchat-space / Langchain-Chatchat

Langchain-Chatchat(原Langchain-ChatGLM)基于 Langchain 与 ChatGLM, Qwen 与 Llama 等语言模型的 RAG 与 Agent 应用 | Langchain-Chatchat (formerly langchain-ChatGLM), local knowledge based LLM (like ChatGLM, Qwen and Llama) RAG and Agent app with langchain
Apache License 2.0
31.83k stars 5.55k forks source link

RuntimeError: Task <Task pending name='Task-820' coro=<RequestResponseCycle.run_asgi() running at /data/zhouyj/IntQA/env/lib/python3.9/site-packages/uvicorn/protocols/http/httptools_impl.py:419> cb=[set.discard()]> got Future <Future pending> attached to a different loop 2024-04-12 14:56:25 | INFO | httpx | HTTP Request: POST http://127.0.0.1:21006/worker_generate_stream "HTTP/1.1 500 Internal Server Error" #3739

Closed SwordHG closed 6 months ago

SwordHG commented 6 months ago

版本是0.29版本 调用的是在线的qwen-turbo 基于知识的问答,当并发大于5的时候,报错 ERROR: Exception in ASGI application Traceback (most recent call last): File "/data/zhouyj/IntQA/env/lib/python3.9/site-packages/uvicorn/protocols/http/httptools_impl.py", line 419, in run_asgi result = await app( # type: ignore[func-returns-value] File "/data/zhouyj/IntQA/env/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py", line 84, in call return await self.app(scope, receive, send) File "/data/zhouyj/IntQA/env/lib/python3.9/site-packages/fastapi/applications.py", line 1054, in call await super().call(scope, receive, send) File "/data/zhouyj/IntQA/env/lib/python3.9/site-packages/starlette/applications.py", line 116, in call await self.middleware_stack(scope, receive, send) File "/data/zhouyj/IntQA/env/lib/python3.9/site-packages/starlette/middleware/errors.py", line 186, in call raise exc File "/data/zhouyj/IntQA/env/lib/python3.9/site-packages/starlette/middleware/errors.py", line 164, in call await self.app(scope, receive, _send) File "/data/zhouyj/IntQA/env/lib/python3.9/site-packages/starlette/middleware/exceptions.py", line 62, in call await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send) File "/data/zhouyj/IntQA/env/lib/python3.9/site-packages/starlette/_exception_handler.py", line 55, in wrapped_app raise exc File "/data/zhouyj/IntQA/env/lib/python3.9/site-packages/starlette/_exception_handler.py", line 44, in wrapped_app await app(scope, receive, sender) File "/data/zhouyj/IntQA/env/lib/python3.9/site-packages/starlette/routing.py", line 746, in call await route.handle(scope, receive, send) File "/data/zhouyj/IntQA/env/lib/python3.9/site-packages/starlette/routing.py", line 288, in handle await self.app(scope, receive, send) File "/data/zhouyj/IntQA/env/lib/python3.9/site-packages/starlette/routing.py", line 75, in app await wrap_app_handling_exceptions(app, request)(scope, receive, send) File "/data/zhouyj/IntQA/env/lib/python3.9/site-packages/starlette/_exception_handler.py", line 55, in wrapped_app raise exc File "/data/zhouyj/IntQA/env/lib/python3.9/site-packages/starlette/_exception_handler.py", line 44, in wrapped_app await app(scope, receive, sender) File "/data/zhouyj/IntQA/env/lib/python3.9/site-packages/starlette/routing.py", line 70, in app response = await func(request) File "/data/zhouyj/IntQA/env/lib/python3.9/site-packages/fastapi/routing.py", line 299, in app raise e File "/data/zhouyj/IntQA/env/lib/python3.9/site-packages/fastapi/routing.py", line 294, in app raw_response = await run_endpoint_function( File "/data/zhouyj/IntQA/env/lib/python3.9/site-packages/fastapi/routing.py", line 191, in run_endpoint_function return await dependant.call(**values) File "/data/zhouyj/IntQA/env/lib/python3.9/site-packages/fastchat/serve/base_model_worker.py", line 196, in api_generate_stream await acquire_worker_semaphore() File "/usr/local/python3.9/lib/python3.9/asyncio/locks.py", line 417, in acquire await fut RuntimeError: Task <Task pending name='Task-820' coro=<RequestResponseCycle.run_asgi() running at /data/zhouyj/IntQA/env/lib/python3.9/site-packages/uvicorn/protocols/http/httptools_impl.py:419> cb=[set.discard()]> got Future attached to a different loop 2024-04-12 14:56:25 | INFO | httpx | HTTP Request: POST http://127.0.0.1:21006/worker_generate_stream "HTTP/1.1 500 Internal Server Error"

这个问题怎么解决,现在qwen-turbo支持qpm为500,想要支持qpm500的并发

dosubot[bot] commented 6 months ago

The error you're encountering is due to asyncio tasks being attached to a different event loop than the one currently running. This is a common issue when there's a mismatch between the event loop used by FastAPI/Uvicorn and other asyncio tasks or background tasks in your application. Here are steps to address this:

  1. Ensure all asynchronous operations are correctly awaited and compatible with FastAPI's event loop.
  2. If your application creates new event loops, use asyncio.set_event_loop(new_loop) to set the newly created loop as the current event loop before scheduling tasks on it.
  3. Review FastAPI's background tasks to ensure they don't inadvertently create a new event loop or run in a different loop.
  4. Increase Uvicorn/Gunicorn workers to distribute the load, especially since you're aiming for 500 queries per minute. Use the --workers argument to specify the number of worker processes.
  5. Implement connection pooling for database access and ensure your database client library is async-compatible.
  6. Optimize your async code to avoid blocking operations and ensure non-blocking behavior.
  7. Implement rate limiting to manage the load and prevent overloading your application.
  8. Perform load testing to identify and address any concurrency issues or bottlenecks.

For more detailed guidance, refer to the FastAPI documentation and the Uvicorn documentation. This approach should help you resolve the issue and scale your application to handle the desired load.

Sources

#### About Dosu This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot.
SwordHG commented 6 months ago

已解决,设置并发数就能解决

ShAw7ock commented 5 months ago

已解决,设置并发数就能解决

你好,请问能详细描述一下解决方案吗?我通过增大args.limit_worker_concurrency的设置方法还是会遇到上述问题,并发数超过5个后,就会报got Future <Future pending> attached to a different loop的问题

SwordHG commented 5 months ago

已解决,设置并发数就能解决

你好,请问能详细描述一下解决方案吗?我通过增大args.limit_worker_concurrency的设置方法还是会遇到上述问题,并发数超过5个后,就会报got Future <Future pending> attached to a different loop的问题

那你的问题和我应该不一样

ShAw7ock commented 5 months ago

那你的问题和我应该不一样

因为我也是调用zhipu-api的在线接口,但超过5个并发数量后也会出现跟你的issue描述中一样的报错信息。

那请问你的解决方案是什么呢?就是像我描述的增大args.limit_worker_concurrency(初始默认值是5)后就解决了并发数量的问题吗?

ShAw7ock commented 5 months ago

那你的问题和我应该不一样

因为我也是调用zhipu-api的在线接口,但超过5个并发数量后也会出现跟你的issue描述中一样的报错信息。

那请问你的解决方案是什么呢?就是像我描述的增大args.limit_worker_concurrency(初始默认值是5)后就解决了并发数量的问题吗?

已解决。

因为我们团队有一个魔改版的Langchain-Chatchat,删减&增加了很多功能、调整了大部分的代码结构,故在处理API ModelWorker的时候,args.limit_worker_concurrency没有正确读取到配置文件中的参数设置。

修改后压力测试没有问题。

SwordHG commented 5 months ago

那你的问题和我应该不一样

因为我也是调用zhipu-api的在线接口,但超过5个并发数量后也会出现跟你的issue描述中一样的报错信息。 那请问你的解决方案是什么呢?就是像我描述的增大args.limit_worker_concurrency(初始默认值是5)后就解决了并发数量的问题吗?

已解决。

因为我们团队有一个魔改版的Langchain-Chatchat,删减&增加了很多功能、调整了大部分的代码结构,故在处理API ModelWorker的时候,args.limit_worker_concurrency没有正确读取到配置文件中的参数设置。

修改后压力测试没有问题。

原来是自己挖的坑,互换个联系方式吧

Nancy7zt commented 3 months ago

可以一起沟通一下吗