在 Jython 2.5 中同时执行多个函数
Execute multiple functions at same time in Jython 2.5
我正在尝试同时 运行 多个 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 函数接口这一事实。
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()
执行相同操作的 Python 方法是
import subprocess, threading
threading.Thread(target=lambda: subprocess.call(["python","test1.py"])).start()
threading.Thread(target=lambda: subprocess.call(["python","test2.py"])).start()
老实说,我会使用 multiprocessing
而不是 threading
,但我不确定 Jython 是否支持它。
我正在尝试同时 运行 多个 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 函数接口这一事实。
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()
执行相同操作的 Python 方法是
import subprocess, threading
threading.Thread(target=lambda: subprocess.call(["python","test1.py"])).start()
threading.Thread(target=lambda: subprocess.call(["python","test2.py"])).start()
老实说,我会使用 multiprocessing
而不是 threading
,但我不确定 Jython 是否支持它。