[英]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()
Pythonic的方法可以做到這一點
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是否支持它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.