JPype在线程中无法工作?

4 投票
1 回答
3272 浏览
提问于 2025-04-17 04:49

我测试了一下JPype,写了一个简单的“你好,世界”的代码:

jpype.java.lang.System.out.println("hello world")

在一个“主”python程序里,这段代码运行得很好。不过,如果在一个正在运行的线程里,我得到了以下的反馈:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007f5272aeb226, pid=7888, tid=139991902578432
#
# JRE version: 7.0_01-b08
# Java VM: Java HotSpot(TM) 64-Bit Server VM (21.1-b02 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# C  [_jpype.so+0x33226]  JPJavaEnv::FindClass(char const*)+0x36
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /home/imedia/NTsvm/src/server/nlp/hs_err_pid7888.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.sun.com/bugreport/crash.jsp
#
Aborted

1 个回答

9

引用自JPype文档

Python线程

大多数情况下,Python的线程是基于操作系统的线程(也就是posix线程),可以正常工作。唯一需要记住的是,在线程内部要调用jpype.attachThreadToJVM(),这样才能让这个线程使用Java虚拟机(JVM)。如果是你没有自己启动的线程,可以用isThreadAttachedToJVM()来检查这个线程是否已经连接到JVM。

希望这对你有帮助

撰写回答