Skip to content

Commit f4290dd

Browse files
authored
Fix the fastapi cli startup event loop (#602)
1 parent 1e54b0b commit f4290dd

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

backend/plugin/tools.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,14 @@ def parse_plugin_config() -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
8585
app_plugins = []
8686

8787
# 事件循环嵌套: https://pypi.org/project/nest-asyncio/
88-
loop = asyncio.get_running_loop()
89-
nest_asyncio.apply(loop)
88+
try:
89+
loop = asyncio.get_running_loop()
90+
except RuntimeError:
91+
loop = asyncio.get_event_loop()
92+
else:
93+
nest_asyncio.apply()
9094

91-
plugin_status = asyncio.run(redis_client.hgetall(f'{settings.PLUGIN_REDIS_PREFIX}:status')) # type: ignore
95+
plugin_status = loop.run_until_complete(redis_client.hgetall(f'{settings.PLUGIN_REDIS_PREFIX}:status')) # type: ignore
9296
if not plugin_status:
9397
plugin_status = {}
9498

@@ -118,12 +122,12 @@ def parse_plugin_config() -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
118122
data['plugin']['name'] = plugin
119123

120124
# 缓存插件信息
121-
asyncio.create_task(
125+
loop.create_task(
122126
redis_client.set(f'{settings.PLUGIN_REDIS_PREFIX}:info:{plugin}', json.dumps(data, ensure_ascii=False))
123127
)
124128

125129
# 缓存插件状态
126-
asyncio.create_task(redis_client.hset(f'{settings.PLUGIN_REDIS_PREFIX}:status', mapping=plugin_status))
130+
loop.create_task(redis_client.hset(f'{settings.PLUGIN_REDIS_PREFIX}:status', mapping=plugin_status))
127131

128132
return extra_plugins, app_plugins
129133

0 commit comments

Comments
 (0)