Skip to content

Fix async install plugin dependencies for windows #549

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/app/admin/api/v1/sys/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ async def install_plugin(file: Annotated[UploadFile, File()]) -> ResponseModel:
members.append(member)
zf.extractall(PLUGIN_DIR, members)
if os.path.exists(os.path.join(full_plugin_path, 'requirements.txt')):
await install_requirements_async(False)
await install_requirements_async()

return response_base.success()

Expand Down
54 changes: 11 additions & 43 deletions backend/plugin/tools.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import asyncio
import inspect
import os
import subprocess
import sys
import warnings

from asyncio import subprocess as async_subprocess

import rtoml

from fastapi import APIRouter
from starlette.concurrency import run_in_threadpool

from backend.core.conf import settings
from backend.core.path_conf import PLUGIN_DIR
Expand Down Expand Up @@ -156,49 +154,19 @@ def install_requirements() -> None:
continue
else:
try:
subprocess.run([sys.executable, '-m', 'ensurepip', '--upgrade'])
pip_requirements = [sys.executable, '-m', 'pip', 'install', '-r', requirements_file]
ensurepip_install = [sys.executable, '-m', 'ensurepip', '--upgrade']
pip_install = [sys.executable, '-m', 'pip', 'install', '-r', requirements_file]
if settings.PLUGIN_PIP_CHINA:
pip_requirements.extend(['-i', settings.PLUGIN_PIP_INDEX_URL])
subprocess.check_call(pip_requirements)
pip_install.extend(['-i', settings.PLUGIN_PIP_INDEX_URL])
subprocess.check_call(ensurepip_install)
subprocess.check_call(pip_install)
except subprocess.CalledProcessError as e:
raise PluginInjectError(f'插件 {plugin} 依赖安装失败:{e}') from e
raise PluginInjectError(f'插件 {plugin} 依赖安装失败:{e.stderr}') from e


async def install_requirements_async(wait: bool = True) -> None:
async def install_requirements_async() -> None:
"""
异步安装插件依赖

:param wait: 是否等待结果并校验,开启将造成 IO 阻塞
:return:
异步安装插件依赖(由于 Windows 平台限制,无法实现完美的全异步方案),详情:
https://stackoverflow.com/questions/44633458/why-am-i-getting-notimplementederror-with-async-and-await-on-windows
"""
plugins = get_plugins()
for plugin in plugins:
requirements_file = os.path.join(PLUGIN_DIR, plugin, 'requirements.txt')
if not os.path.exists(requirements_file):
continue
else:
ensurepip_process = await async_subprocess.create_subprocess_exec(
sys.executable,
'-m',
'ensurepip',
'--upgrade',
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
if wait:
_, ensurepip_stderr = await ensurepip_process.communicate()
if ensurepip_process.returncode != 0:
raise PluginInjectError(f'ensurepip 安装失败:{ensurepip_stderr}')
pip_requirements = [sys.executable, '-m', 'pip', 'install', '-r', requirements_file]
if settings.PLUGIN_PIP_CHINA:
pip_requirements.extend(['-i', settings.PLUGIN_PIP_INDEX_URL])
pip_process = await async_subprocess.create_subprocess_exec(
*pip_requirements,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
if wait:
_, pip_stderr = await pip_process.communicate()
if pip_process.returncode != 0:
raise PluginInjectError(f'插件 {plugin} 依赖包安装失败:{pip_stderr}')
await run_in_threadpool(install_requirements)