gpt4 book ai didi

java - 在 Jython 2.5 中同时执行多个函数

转载 作者:行者123 更新时间:2023-12-02 02:10:24 25 4
gpt4 key购买 nike

我尝试同时运行多个 Jython 文件,以便可以使用我的 PC 多处理器(特别是在 Hyperion 工作区中的 FDM 中执行此操作)

有什么办法可以做到这一点吗?

我尝试过通过Java实现,但它不识别线程函数,也尝试过通过Python实现,而且这个版本的Jython没有并发库,无法导入它。

import os
import sys
from java.io import *
from java.util import *
from java import *
from java.lang import *
from threading import *
import java.util.logging.Level;
import java.util.logging.Logger;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;


new Thread() {
public void run() {
java.lang.Runtime.getRuntime().exec("python test1.py")
}
}.start()
new Thread() {
public void run() {
java.lang.Runtime.getRuntime().exec("python test2.py")
}
}.start()
new Thread() {
public void run() {
java.lang.Runtime.getRuntime().exec("python test3.py")
}
}.start()

错误:

File "E:\Oracle\Middleware\EPMSystem11R1\products\FinancialDataQuality\Applications\FDMEE/data/scripts/custom/test.py", line 15
new Thread() {
^
SyntaxError: mismatched input 'Thread' expecting NEWLINE

最佳答案

您不能在 python 代码中使用 Java 语法。即使您使用 Jython 运行它。

您可以利用 Jython 将 python 函数转换为 Java 函数接口(interface)的事实。

from java.lang import Thread, Runtime


Thread(lambda: Runtime.getRuntime().exec("python test1.py")).start()
Thread(lambda: Runtime.getRuntime().exec("python test2.py")).start()
Thread(lambda: Runtime.getRuntime().exec("python test3.py")).start()

Pythonic 的方式来做同样的事情

import subprocess, threading

threading.Thread(target=lambda: subprocess.call(["python","test1.py"])).start()
threading.Thread(target=lambda: subprocess.call(["python","test2.py"])).start()

老实说,我会使用多处理而不是线程,但我不确定Jython是否支持它。

关于java - 在 Jython 2.5 中同时执行多个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57331004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com