fix: 移除python-embedding依赖,只使用标准Polyglot API

- 移除pom.xml中的python-embedding依赖
- 修改PyContextPool使用标准Polyglot API创建Engine和Context
- 更新checkGraalPyAvailability方法使用标准API检查
- 测试验证通过
This commit is contained in:
q
2026-01-11 07:27:10 +08:00
parent 2e5e679cea
commit 5883c9f7fd
2 changed files with 28 additions and 32 deletions

View File

@@ -119,11 +119,6 @@
<version>${graalpy.version}</version> <version>${graalpy.version}</version>
<type>pom</type> <type>pom</type>
</dependency> </dependency>
<dependency>
<groupId>org.graalvm.python</groupId>
<artifactId>python-embedding</artifactId>
<version>${graalpy.version}</version>
</dependency>
<!-- Compression (Brotli) --> <!-- Compression (Brotli) -->
<dependency> <dependency>

View File

@@ -134,28 +134,22 @@ public class PyContextPool {
private PyContextPool() { private PyContextPool() {
log.info("初始化GraalPy Context池..."); log.info("初始化GraalPy Context池...");
// 创建共享Engine - 优先使用 GraalPyResources // 创建共享Engine - 使用标准Polyglot API
Engine engine = null; Engine engine = null;
try { try {
// 使用 GraalPyResources 创建,这是推荐的方式 engine = Engine.newBuilder()
var tempContext = org.graalvm.python.embedding.GraalPyResources.contextBuilder()
.option("engine.WarnInterpreterOnly", "false") .option("engine.WarnInterpreterOnly", "false")
.build(); .build();
engine = tempContext.getEngine();
tempContext.close(); // 验证Python语言是否可用
log.info("Engine创建成功GraalPyResources模式"); if (!engine.getLanguages().containsKey("python")) {
} catch (Exception e) { throw new IllegalStateException("Python语言不可用请检查GraalPy依赖配置");
log.warn("GraalPyResources模式创建Engine失败尝试标准方式: {}", e.getMessage());
try {
engine = Engine.newBuilder()
.option("engine.WarnInterpreterOnly", "false")
.build();
log.info("Engine创建成功标准模式");
} catch (Exception e2) {
log.error("标准模式创建Engine也失败: {}", e2.getMessage());
checkGraalPyAvailability();
throw new RuntimeException("无法初始化GraalPy Engine请确保GraalPy依赖正确配置", e2);
} }
log.info("Engine创建成功可用语言: {}", engine.getLanguages().keySet());
} catch (Exception e) {
log.error("创建Engine失败: {}", e.getMessage());
checkGraalPyAvailability();
throw new RuntimeException("无法初始化GraalPy Engine请确保GraalPy依赖正确配置", e);
} }
this.sharedEngine = engine; this.sharedEngine = engine;
@@ -279,9 +273,9 @@ public class PyContextPool {
.option("python.ForceImportSite", "false") .option("python.ForceImportSite", "false")
.build(); .build();
} catch (Exception e) { } catch (Exception e) {
log.warn("使用共享Engine创建Context失败尝试使用GraalPyResources: {}", e.getMessage()); log.warn("使用共享Engine创建Context失败尝试使用共享Engine: {}", e.getMessage());
// 使用 GraalPyResources 作为备选 // 使用共享Engine作为备选
context = org.graalvm.python.embedding.GraalPyResources.contextBuilder() context = Context.newBuilder("python")
.allowHostAccess(HostAccess.newBuilder(HostAccess.EXPLICIT) .allowHostAccess(HostAccess.newBuilder(HostAccess.EXPLICIT)
.allowArrayAccess(true) .allowArrayAccess(true)
.allowListAccess(true) .allowListAccess(true)
@@ -298,6 +292,9 @@ public class PyContextPool {
.allowHostFileAccess(false) .allowHostFileAccess(false)
.allowHostSocketAccess(false) .allowHostSocketAccess(false)
.build()) .build())
.option("engine.WarnInterpreterOnly", "false")
.option("python.PythonHome", "")
.option("python.ForceImportSite", "false")
.build(); .build();
} }
@@ -484,24 +481,28 @@ public class PyContextPool {
Class.forName("org.graalvm.python.embedding.GraalPyResources"); Class.forName("org.graalvm.python.embedding.GraalPyResources");
log.info("✓ org.graalvm.python.embedding.GraalPyResources 类存在"); log.info("✓ org.graalvm.python.embedding.GraalPyResources 类存在");
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
log.error("✗ org.graalvm.python.embedding.GraalPyResources 类不存在 - 缺少 python-embedding 依赖"); log.warn(" python-embedding 类不存在(可选依赖");
} }
// 尝试列出可用语言 // 尝试列出可用语言
try { try {
log.info("尝试使用 GraalPyResources 创建 Context..."); log.info("尝试使用标准 Polyglot API 创建 Context...");
var context = org.graalvm.python.embedding.GraalPyResources.createContext(); try (Engine engine = Engine.create()) {
log.info("✓ GraalPyResources 创建 Context 成功"); log.info(" 可用语言: {}", engine.getLanguages().keySet());
context.close(); if (engine.getLanguages().containsKey("python")) {
log.info("✓ Python 语言可用");
} else {
log.error("✗ Python 语言不可用");
}
}
} catch (Exception e) { } catch (Exception e) {
log.error("GraalPyResources 创建 Context 失败: {}", e.getMessage()); log.error("创建 Engine 失败: {}", e.getMessage());
} }
log.error("================================"); log.error("================================");
log.error("请检查以下依赖是否正确配置:"); log.error("请检查以下依赖是否正确配置:");
log.error(" 1. org.graalvm.polyglot:polyglot"); log.error(" 1. org.graalvm.polyglot:polyglot");
log.error(" 2. org.graalvm.polyglot:python (type=pom)"); log.error(" 2. org.graalvm.polyglot:python (type=pom)");
log.error(" 3. org.graalvm.python:python-embedding");
log.error("================================"); log.error("================================");
} }