Bug: 报错There is no current event loop in thread 'MainThread'.
Closed this issue · 10 comments
python版本升级3.10.10解决问题
在Python 3.8和3.9版本中,尝试在没有活动的事件循环时创建asyncio.Semaphore
对象会引发错误。这是因为asyncio.Semaphore
需要在其内部使用事件循环来处理异步操作,而如果在当前线程中没有运行事件循环,Python就无法为Semaphore
提供所需的上下文。从Python 3.10开始,asyncio
模块在某些情况下允许在没有活动事件循环的情况下执行操作,这可能是为了增强灵活性和兼容性。因此,当在Python 3.8或3.9版本中遇到这个问题时,通常是因为代码尝试在一个尚未启动事件循环的线程中进行异步操作。
在 Python 3.8.10 的 Ubuntu 中运行最新版的 bilichat 5.0.0a9 出现了同样的问题
第一次报错:
12-20 11:10:34 [INFO] nonebot_plugin_bilichat | Cache folder for nonebot_plugin_bilichat is located at /home/Jerry_FaGe/.cache/nonebot2/nonebot_plugin_bilichat
12-20 11:10:34 [INFO] nonebot_plugin_bilichat | Data folder for nonebot_plugin_bilichat is located at /home/Jerry_FaGe/.local/share/nonebot2/nonebot_plugin_bilichat
12-20 11:10:34 [INFO] nonebot_plugin_bilichat | Use JsonFile as cache service
12-20 11:10:34 [INFO] nonebot_plugin_bilichat | Loading OpenAI Token enc model
12-20 11:10:34 [SUCCESS] nonebot_plugin_bilichat | Enc model cl100k_base load successfully
12-20 11:10:35 [ERROR] nonebot | Failed to import "nonebot_plugin_bilichat"
Traceback (most recent call last):
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/bin/nb", line 8, in <module>
sys.exit(main())
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/click/core.py", line 1157, in __call__
return self.main(*args, **kwargs)
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/click/core.py", line 1078, in main
rv = self.invoke(ctx)
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/click/core.py", line 1688, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/click/core.py", line 1434, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/click/core.py", line 783, in invoke
return __callback(*args, **kwargs)
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nb_cli/commands/main.py", line 30, in run
run_bot(file, app)
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nb_cli/handlers/deploy.py", line 20, in run_bot
module = importlib.import_module(module_name)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 848, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/Jerry_FaGe/bot/KyaRuChan/./bot.py", line 29, in <module>
nonebot.load_from_toml("pyproject.toml")
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot/plugin/load.py", line 130, in load_from_toml
return load_all_plugins(plugins, plugin_dirs)
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot/plugin/load.py", line 64, in load_all_plugins
return manager.load_all_plugins()
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot/plugin/manager.py", line 176, in load_all_plugins
return set(
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot/plugin/manager.py", line 177, in <genexpr>
filter(None, (self.load_plugin(name) for name in self.available_plugins))
> File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot/plugin/manager.py", line 142, in load_plugin
module = importlib.import_module(name)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot/plugin/manager.py", line 242, in exec_module
super().exec_module(module)
File "<frozen importlib._bootstrap_external>", line 848, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot_plugin_bilichat/__init__.py", line 74, in <module>
from . import adapters, api, commands # noqa: F401, E402
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot_plugin_bilichat/api/__init__.py", line 7, in <module>
from . import base, bilibili_auth, subs_config # noqa: F401
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot_plugin_bilichat/api/subs_config.py", line 9, in <module>
from ..subscribe.manager import SubscriptionSystem
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot_plugin_bilichat/subscribe/__init__.py", line 8, in <module>
from .dynamic import fetch_dynamics_grpc, fetch_dynamics_rest
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot_plugin_bilichat/subscribe/dynamic.py", line 15, in <module>
from ..adapters.base_content_parsing import check_cd
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot_plugin_bilichat/adapters/base_content_parsing.py", line 29, in <module>
lock = asyncio.Lock()
File "/usr/lib/python3.8/asyncio/locks.py", line 164, in __init__
self._loop = events.get_event_loop()
File "/usr/lib/python3.8/asyncio/events.py", line 639, in get_event_loop
raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'MainThread'.
第二次报错(每次运行 nb run 都会重启一次,我不知道我是特例还是就这样设计的):
12-20 11:11:01 [INFO] nonebot_plugin_bilichat | Use JsonFile as cache service
12-20 11:11:01 [ERROR] nonebot | Failed to import "nonebot_plugin_bilichat"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python3.8/multiprocessing/spawn.py", line 116, in spawn_main
exitcode = _main(fd, parent_sentinel)
...
...
...
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot_plugin_bilichat/__init__.py", line 74, in <module>
from . import adapters, api, commands # noqa: F401, E402
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot_plugin_bilichat/api/__init__.py", line 7, in <module>
from . import base, bilibili_auth, subs_config # noqa: F401
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot_plugin_bilichat/api/subs_config.py", line 9, in <module>
from ..subscribe.manager import SubscriptionSystem
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot_plugin_bilichat/subscribe/__init__.py", line 8, in <module>
from .dynamic import fetch_dynamics_grpc, fetch_dynamics_rest
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot_plugin_bilichat/subscribe/dynamic.py", line 15, in <module>
from ..adapters.base_content_parsing import check_cd
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot_plugin_bilichat/adapters/base_content_parsing.py", line 11, in <module>
from ..content import Column, Dynamic, Video
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot_plugin_bilichat/content/__init__.py", line 1, in <module>
from .column import Column
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot_plugin_bilichat/content/column.py", line 12, in <module>
from ..lib.draw.column import draw_column
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot_plugin_bilichat/lib/draw/__init__.py", line 5, in <module>
font_semibold = str(get_font_sync("sarasa-mono-sc-semibold.ttf"))
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot_plugin_bilichat/lib/fonts_provider.py", line 53, in get_font_sync
return loop.run_until_complete(get_font(font))
File "uvloop/loop.pyx", line 1511, in uvloop.loop.Loop.run_until_complete
raise
File "uvloop/loop.pyx", line 1504, in uvloop.loop.Loop.run_until_complete
self.run_forever()
File "uvloop/loop.pyx", line 1377, in uvloop.loop.Loop.run_forever
self._run(mode)
File "uvloop/loop.pyx", line 518, in uvloop.loop.Loop._run
raise RuntimeError('this event loop is already running.')
RuntimeError: this event loop is already running.
未知问题,lock的正确用法应该是包含在loop外创建的,而且我这里3.8.10无法复现,但有些情况下却能稳定复现,暂不知道成因
Ubuntu Python 3.8.10 运行新版 bilichat 5.0.1,第一次启动貌似正常了,但 nb 自动重启后会报错
第一次:
12-21 08:32:09 [INFO] nonebot_plugin_bilichat | Use JsonFile as cache service
12-21 08:32:09 [INFO] nonebot_plugin_bilichat | Loading OpenAI Token enc model
12-21 08:32:09 [SUCCESS] nonebot_plugin_bilichat | Enc model cl100k_base load successfully
12-21 08:32:10 [SUCCESS] nonebot_plugin_bilichat | OneBot V11 adapter was loaded successfully
12-21 08:32:11 [SUCCESS] nonebot_plugin_bilichat | OneBot V12 adapter was loaded successfully
12-21 08:32:11 [SUCCESS] nonebot_plugin_bilichat |
****************************************************************************************
* *
* SETTING UP BILICHAT WebUI AT *
* *
* http://127.0.0.1:8080/bilichat/ *
* *
* WARNING: Bilichat WebUI is currently running on default path. Please consider *
* to use different path via adding config `bilichat_webui_path` in .env file. *
* *
****************************************************************************************
12-21 08:32:11 [WARNING] nonebot | Duplicated prefix rule "关注"
12-21 08:32:12 [SUCCESS] nonebot | Succeeded to load plugin "nonebot_plugin_bilichat"
第二次(只放主要部分):
12-21 08:32:47 [INFO] nonebot_plugin_bilichat | Use JsonFile as cache service
12-21 08:32:47 [INFO] nonebot_plugin_bilichat | Loading OpenAI Token enc model
/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot_plugin_bilichat/adapters/__init__.py:8: RuntimeWarning: coroutine 'encoding_for_model' was never awaited
pass
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
12-21 08:32:47 [INFO] nonebot_plugin_bilichat | Loading OpenAI Token enc model
/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot_plugin_bilichat/adapters/__init__.py:15: RuntimeWarning: coroutine 'encoding_for_model' was never awaited
pass
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
12-21 08:32:48 [INFO] nonebot_plugin_bilichat | Loading OpenAI Token enc model
12-21 08:32:48 [ERROR] nonebot | Failed to import "nonebot_plugin_bilichat"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python3.8/multiprocessing/spawn.py", line 116, in spawn_main
exitcode = _main(fd, parent_sentinel)
...
...
...
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot_plugin_bilichat/__init__.py", line 74, in <module>
from . import adapters, api, commands # noqa: F401, E402
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot_plugin_bilichat/api/__init__.py", line 18, in <module>
from . import bilibili_auth, subs_config # noqa: F401
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot_plugin_bilichat/api/subs_config.py", line 10, in <module>
from ..subscribe.manager import SubscriptionSystem
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot_plugin_bilichat/subscribe/__init__.py", line 8, in <module>
from .dynamic import fetch_dynamics_grpc, fetch_dynamics_rest
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot_plugin_bilichat/subscribe/dynamic.py", line 15, in <module>
from ..adapters.base_content_parsing import check_cd
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot_plugin_bilichat/adapters/base_content_parsing.py", line 17, in <module>
from ..summary import summarization
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot_plugin_bilichat/summary/__init__.py", line 6, in <module>
from .openai_summarise import openai_summarization
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot_plugin_bilichat/summary/openai_summarise.py", line 10, in <module>
from .openai import get_small_size_transcripts, get_summarise_prompt, openai_req
File "/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot_plugin_bilichat/summary/openai.py", line 14, in <module>
tiktoken_enc = asyncio.run(tiktoken_async.encoding_for_model(plugin_config.bilichat_openai_model))
File "/usr/lib/python3.8/asyncio/runners.py", line 33, in run
raise RuntimeError(
RuntimeError: asyncio.run() cannot be called from a running event loop
/home/Jerry_FaGe/bot/KyaRuChan/.venv/lib/python3.8/site-packages/nonebot/plugin/manager.py:169: RuntimeWarning: coroutine 'encoding_for_model' was never awaited
logger.opt(colors=True, exception=e).error(
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
自动重启本来就不在考虑范围内,请不要发送因自动重启造成的错误,此Issue保留的目的是找出为什么导致此理论上不应该出现的错误
那有没有一种可能,导致某些错误的原因就是开启了 fastapi_reload
呢?
不知道您有没有看过这个:nonebot 文档中对于 fastapi_reload 的警告
我觉得多少有点关系
那有没有一种可能,导致某些错误的原因就是开启了
fastapi_reload
呢? 不知道您有没有看过这个:nonebot 文档中对于 fastapi_reload 的警告 我觉得多少有点关系
- 这是一个单独且默认关闭的配置项,绝大多数情况下用户不会主动开启
- 此错误不仅发生在 Windows 平台,有的用户甚至在 docker 中可以复现
- 此问题发生在加载插件的阶段,此时 nonebot 未开启 uvicorn server,基本可以断言与 fastapi 无关,更有可能是 asyncio 自身的问题