简体   繁体   English

在Jython 2.5中同时执行多项功能

[英]Execute multiple functions at same time in Jython 2.5

I'm trying to run multiple Jython files at the same time so that I can use my PC multiprocessor (specifically doing this in FDM in Hyperion's Workspace) 我试图同时运行多个Jython文件,以便可以使用PC多处理器(特别是在Hyperion工作区的FDM中执行此操作)

Is there any way I could do this? 有什么办法可以做到吗?

I've tried to do it through Java, but it doesn't recognize the thread function, also tried through Python, and this version of Jython doesn't have the concurrency library, and not able to import it. 我试图通过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()

Errors: 错误:

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

You cannot use Java syntax in python code. 您不能在python代码中使用Java语法。 Even if you're running it with Jython. 即使您与Jython一起运行。

You can use the fact that Jython will convert python functions to Java functional interface. 您可以使用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 way to do the same would be Pythonic的方法可以做到这一点

import subprocess, threading

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

To be honest I would use multiprocessing instead of threading , but I am not sure if Jython supports it. 老实说,我将使用multiprocessing而不是threading ,但是我不确定Jython是否支持它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM