在从Python发送通知之前,您可以使用一个永不结束的线程创建一个类java程序.
这样lib就可以保存在内存中,并可以从Python程序中访问.
这个类可能是这样的(添加你需要的lib import / init):
public class TestPy {
private Thread thread;
public void die() {
synchronized (thread) {
thread.interrupt();
}
}
public TestPy() {
thread = new Thread(){
public void run() {
try {
while (!Thread.interrupted()) {
Thread.sleep(500);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
};
thread.start();
}
public static void main(String[] args) {
TestPy tp = new TestPy();
GatewayServer server = new GatewayServer(tp);
server.start();
}
}
你必须启动java程序,使用lib,然后使用die()方法杀死Python中的java程序:
gateway = JavaGateway()
do your stuff here using the lib
tp = gateway.entry_point
tp.die()