Skip to content
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

Event loop is closed #65

Closed
ZohnSun opened this issue Apr 2, 2024 · 21 comments
Closed

Event loop is closed #65

ZohnSun opened this issue Apr 2, 2024 · 21 comments
Assignees
Labels
bug Something isn't working solved For those issues already solved but stay open for more user can see

Comments

@ZohnSun
Copy link

ZohnSun commented Apr 2, 2024

pycharm中执行样例总是报线程问题,这个异步是否需要优化一下

Exception ignored in: <function _ProactorBasePipeTransport.del at 0x000001B09421C4C0>
Traceback (most recent call last):
File "D:\Python\Python39\lib\asyncio\proactor_events.py", line 116, in del
self.close()
File "D:\Python\Python39\lib\asyncio\proactor_events.py", line 108, in close
self._loop.call_soon(self._call_connection_lost, None)
File "D:\Python\Python39\lib\asyncio\base_events.py", line 751, in call_soon
self._check_closed()
File "D:\Python\Python39\lib\asyncio\base_events.py", line 515, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

@ZohnSun
Copy link
Author

ZohnSun commented Apr 2, 2024

调用百度样例会出现,智谱没有出现

agent_factory\

.set_settings("current_model", "ERNIE")\

.set_settings("model.ERNIE.auth", {

"aistudio": "xxxxxxxxxxxxxxxxxxxxxxxxxxx",

})

agent = agent_factory.create_agent()
agent.alias_manager.print_alias_info()

result = (
agent_GLM_3.general("输出规定", "必须使用中文进行输出")
# role: agent自身的角色设定信息
.role({
"姓名": "Agently小助手",
"任务": "使用自己的知识为用户解答常见问题",
})
# user_info: agent需要了解的用户相关的信息
.user_info("和你对话的用户是一个只具有Python编程基础知识的入门初学者")
# abstract: 对于之前对话(尤其是较长对话)的总结信息
.abstract(None)
# chat_history: 按照OpenAI消息列格式的对话记录list
## 支持:
## [{ "role": "system", "content": "" },
## { "role": "assistant", "content": "" },
## { "role": "user", "content": "" }]
## 三种角色
.chat_history([])
# input: 和本次请求相关的输入信息
.input({
"question": "请问Python中协程和线程的关系和区别是什么?",
"reply_style_expect": "请用对编程概念零基础的人能理解的方式进行回复"
})
# info: 为本次请求提供的额外补充信息
.info("协程相关关键词", ["asyncio", "coroutine", "event loop"])
.info("线程相关关键词", ["threads", "queue"])
# instruct: 为本次请求提供的行动指导信息
.instruct([
"请使用{reply_style_expect}的回复风格,回复{question}提出的问题",
])
# output: 对本次请求的输出提出格式和内容的要求
.output({
"reply": ("str", "对{question}的直接回复"),
"next_questions": ([
("str",
"根据{reply}内容,结合{user_info}提供的用户信息," +
"给用户推荐的可以进一步提问的问题"
)], "不少于3个"),
})
# start: 用于开始本次主要交互请求
.start()
)

@Maplemx
Copy link
Owner

Maplemx commented Apr 2, 2024

已经关注到这个问题,因为不是必定出现,正在排查出现的时机

@Xls1994
Copy link

Xls1994 commented Apr 3, 2024

我这边也出现了这个问题,使用的是本地部署的 OpenAI 接口的模型,另外,使用 Yi34B 的 API 也会出现此问题。
智谱的确实,目前没有发现问题

@Maplemx
Copy link
Owner

Maplemx commented Apr 5, 2024

我这边也出现了这个问题,使用的是本地部署的 OpenAI 接口的模型,另外,使用 Yi34B 的 API 也会出现此问题。 智谱的确实,目前没有发现问题

请问是每次请求都会出现吗

@Xls1994
Copy link

Xls1994 commented Apr 5, 2024

调用全局工具的时候,几乎都是稳定会触发的

@Maplemx
Copy link
Owner

Maplemx commented Apr 8, 2024

调用全局工具的时候,几乎都是稳定会触发的

可能找到了一个相关问题,请问这个全局工具本身是不是异步的?

@Maplemx
Copy link
Owner

Maplemx commented Apr 8, 2024

pycharm中执行样例总是报线程问题,这个异步是否需要优化一下

Exception ignored in: <function _ProactorBasePipeTransport.del at 0x000001B09421C4C0> Traceback (most recent call last): File "D:\Python\Python39\lib\asyncio\proactor_events.py", line 116, in del self.close() File "D:\Python\Python39\lib\asyncio\proactor_events.py", line 108, in close self._loop.call_soon(self._call_connection_lost, None) File "D:\Python\Python39\lib\asyncio\base_events.py", line 751, in call_soon self._check_closed() File "D:\Python\Python39\lib\asyncio\base_events.py", line 515, in _check_closed raise RuntimeError('Event loop is closed') RuntimeError: Event loop is closed

我在Agently 3.2.2.3版本修正了一些异步的问题,目前本地使用PyCharm 3.9环境下调用相同的代码,或是使用浏览器或者搜索工具,目前都没有遇到Event loop is closed的情况。

可以更新至3.2.2.3再试试

@Xls1994
Copy link

Xls1994 commented Apr 8, 2024

调用全局工具的时候,几乎都是稳定会触发的

可能找到了一个相关问题,请问这个全局工具本身是不是异步的?

不是的,我这边没有做异步处理,是否和这个有关系呢

@Maplemx
Copy link
Owner

Maplemx commented Apr 9, 2024

调用全局工具的时候,几乎都是稳定会触发的

可能找到了一个相关问题,请问这个全局工具本身是不是异步的?

不是的,我这边没有做异步处理,是否和这个有关系呢

我处理了一些涉及异步调用未等待的问题,可以更新到3.2.2.3版本再试试

@Maplemx Maplemx added the bug Something isn't working label Apr 9, 2024
@Maplemx Maplemx self-assigned this Apr 9, 2024
@standing88
Copy link

Agently.AgentFactory().set_settings("current_model", "OpenAI").set_settings("model.OpenAI.auth", { "api_key": "" }).set_settings("model.OpenAI.url", "https://api.moonshot.cn/v1").set_settings("model.OpenAI.options", { "model": "moonshot-v1-8k" })

我用kimi 也很容易触发这个问题
Task exception was never retrieved
future: <Task finished name='Task-116' coro=<AsyncClient.aclose() done, defined at D:\anaconda3\Lib\site-packages\httpx_client.py:1996> exception=RuntimeError('Event loop is closed')>
Traceback (most recent call last):
File "D:\anaconda3\Lib\site-packages\httpx_client.py", line 2006, in aclose
await proxy.aclose()
File "D:\anaconda3\Lib\site-packages\httpx_transports\default.py", line 383, in aclose
await self._pool.aclose()
File "D:\anaconda3\Lib\site-packages\httpcore_async\connection_pool.py", line 324, in aclose
await connection.aclose()
File "D:\anaconda3\Lib\site-packages\httpcore_async\http_proxy.py", line 350, in aclose
await self._connection.aclose()
File "D:\anaconda3\Lib\site-packages\httpcore_async\http11.py", line 253, in aclose
await self._network_stream.aclose()
File "D:\anaconda3\Lib\site-packages\httpcore_backends\anyio.py", line 54, in aclose
await self._stream.aclose()
File "D:\anaconda3\Lib\site-packages\anyio\streams\tls.py", line 168, in aclose
await self.transport_stream.aclose()
File "D:\anaconda3\Lib\site-packages\anyio_backends_asyncio.py", line 1163, in aclose
self._transport.close()
File "D:\anaconda3\Lib\asyncio\selector_events.py", line 839, in close
self._loop.call_soon(self._call_connection_lost, None)
File "D:\anaconda3\Lib\asyncio\base_events.py", line 761, in call_soon
self._check_closed()
File "D:\anaconda3\Lib\asyncio\base_events.py", line 519, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Task exception was never retrieved
future: <Task finished name='Task-117' coro=<AsyncClient.aclose() done, defined at D:\anaconda3\Lib\site-packages\httpx_client.py:1996> exception=RuntimeError('Event loop is closed')>
Traceback (most recent call last):
File "D:\anaconda3\Lib\site-packages\httpx_client.py", line 2006, in aclose
await proxy.aclose()
File "D:\anaconda3\Lib\site-packages\httpx_transports\default.py", line 383, in aclose
await self._pool.aclose()
File "D:\anaconda3\Lib\site-packages\httpcore_async\connection_pool.py", line 324, in aclose
await connection.aclose()
File "D:\anaconda3\Lib\site-packages\httpcore_async\http_proxy.py", line 350, in aclose
await self._connection.aclose()
File "D:\anaconda3\Lib\site-packages\httpcore_async\http11.py", line 253, in aclose
await self._network_stream.aclose()
File "D:\anaconda3\Lib\site-packages\httpcore_backends\anyio.py", line 54, in aclose
await self._stream.aclose()
File "D:\anaconda3\Lib\site-packages\anyio\streams\tls.py", line 168, in aclose
await self.transport_stream.aclose()
File "D:\anaconda3\Lib\site-packages\anyio_backends_asyncio.py", line 1163, in aclose
self._transport.close()
File "D:\anaconda3\Lib\asyncio\selector_events.py", line 839, in close
self._loop.call_soon(self._call_connection_lost, None)
File "D:\anaconda3\Lib\asyncio\base_events.py", line 761, in call_soon
self._check_closed()
File "D:\anaconda3\Lib\asyncio\base_events.py", line 519, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

Task exception was never retrieved
future: <Task finished name='Task-122' coro=<AsyncClient.aclose() done, defined at D:\anaconda3\Lib\site-packages\httpx_client.py:1996> exception=RuntimeError('Event loop is closed')>
Traceback (most recent call last):
File "D:\anaconda3\Lib\site-packages\httpx_client.py", line 2006, in aclose
await proxy.aclose()
File "D:\anaconda3\Lib\site-packages\httpx_transports\default.py", line 383, in aclose
await self._pool.aclose()
File "D:\anaconda3\Lib\site-packages\httpcore_async\connection_pool.py", line 324, in aclose
await connection.aclose()
File "D:\anaconda3\Lib\site-packages\httpcore_async\http_proxy.py", line 350, in aclose
await self._connection.aclose()
File "D:\anaconda3\Lib\site-packages\httpcore_async\http11.py", line 253, in aclose
await self._network_stream.aclose()
File "D:\anaconda3\Lib\site-packages\httpcore_backends\anyio.py", line 54, in aclose
await self._stream.aclose()
File "D:\anaconda3\Lib\site-packages\anyio\streams\tls.py", line 168, in aclose
await self.transport_stream.aclose()
File "D:\anaconda3\Lib\site-packages\anyio_backends_asyncio.py", line 1163, in aclose
self._transport.close()
File "D:\anaconda3\Lib\asyncio\selector_events.py", line 839, in close
self._loop.call_soon(self._call_connection_lost, None)
File "D:\anaconda3\Lib\asyncio\base_events.py", line 761, in call_soon
self._check_closed()
File "D:\anaconda3\Lib\asyncio\base_events.py", line 519, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

@Maplemx
Copy link
Owner

Maplemx commented Apr 12, 2024

进展:根据目前的反馈,怀疑是OpenAI客户端对其他模型的适配存在问题,正在考虑用httpx请求替换掉对OAI客户端的依赖

@Maplemx
Copy link
Owner

Maplemx commented Apr 12, 2024

@Xls1994 @dudu8246 @ZohnSun 可以尝试在代码头部引入nest_asyncio看看能解决吗?

import nest_asyncio
nest_asyncio.apply()

@Maplemx
Copy link
Owner

Maplemx commented Apr 13, 2024

部分测试显示nest_asyncio引入有效果,会在后续版本引入进包

@Maplemx
Copy link
Owner

Maplemx commented Apr 15, 2024

已经将nest_asyncio引入框架内,无需额外设置,自动启用

应该会极大降低event loop is closed的概率

@Maplemx Maplemx added the solved For those issues already solved but stay open for more user can see label Apr 15, 2024
@Maplemx
Copy link
Owner

Maplemx commented Apr 19, 2024

因为nest_asyncio和Gradio有冲突,只能从框架中移出

目前定位到event loop is closed错误很可能是OpenAI客户端的问题,使用pip install -U OpenAI升级到最新版就好

也可以自行引入nest_asyncio降低这个错误出现的概率

@Maplemx Maplemx removed the solved For those issues already solved but stay open for more user can see label May 17, 2024
@Maplemx
Copy link
Owner

Maplemx commented May 17, 2024

This bug still happen time to time so I remove the solved label and will keep on locate it.

But this bug seems have no harm to your process and will not break or stop the process except the annoying console error report...

@zhaopengme
Copy link

还是不行,依旧有这样的错误

@Maplemx
Copy link
Owner

Maplemx commented May 20, 2024

还是不行,依旧有这样的错误

是的,我还在查看问题具体的位置,同时也在改写请求核心,期望后续重构后的大版本更新能解决这个问题

@Maplemx
Copy link
Owner

Maplemx commented May 30, 2024

update in efd937a
publish in version 3.2.2.9

wait to see if this issue can be solved or not.

@Maplemx
Copy link
Owner

Maplemx commented May 30, 2024

updated in 81242f4
published in version 3.2.2.10

local test shows no 'event loop is close' error any more.

please use pip install Agently>=3.2.2.10 to update your Agently package to solve this issue.

@Maplemx
Copy link
Owner

Maplemx commented May 30, 2024

related issue from httpx: encode/httpx#2959

@Maplemx Maplemx added the solved For those issues already solved but stay open for more user can see label May 30, 2024
@Maplemx Maplemx pinned this issue Jun 7, 2024
@Maplemx Maplemx closed this as completed Jun 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working solved For those issues already solved but stay open for more user can see
Projects
None yet
Development

No branches or pull requests

5 participants