在matlab中调用python脚本的多个实例java.lang.Runtime运行时.getRuntime不工作

2024-10-01 02:32:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我在windows 10上运行Matlab2017。 我调用了一个python脚本,它在云上运行一些语音识别任务,如下所示:

 userAuthCode=1;% authentication code for user account to be run on cloud
 cmd = ['C:\Python27\python.exe runASR.py userAuthCode];  
 system(cmd);

当python的语音识别命令在python控制台上运行时,我可以在上面的音频文件中看到python的声音识别命令。 我想做以下事情:

(1)并行执行多个这样的命令。假设,我有2个输入音频文件(每个文件都有不同的音频段),我想运行上述命令2次,但并行,使用不同的进程。我能够创建一个代码段,它应该能够做到:

^{pr2}$

现在,当执行上述代码时,我可以看到数据1的ASRE分数,但不能看到数据2的分数。
变量rc中的退出状态是0,1,,这证实了这一点。 问题是我不知道错误的原因,因为没有打印出来 Matlab。如何从java/Matlab捕获的Python中获取错误消息 让我看一下?在

问题可能是多次调用 ASRE并行(当然有不同的用户帐户)可能不会 支持,但我不知道除非我能看到错误。在

(2)当我独立运行一个命令时,如文章开头所述,我能够看到在Matlab控制台中打印的每个音频段的分数消息,因为它们是从Python获得的。但是,使用java.lang.Runtime运行时.getRuntime()以及相关代码,在Matlab控制台中不显示任何消息。有没有一种方法可以显示这些消息(我假设显示可能是异步的?)在

谢谢
赛迪


Tags: 数据代码命令cmd消息windows错误语音
1条回答
网友
1楼 · 发布于 2024-10-01 02:32:07

一种方法是在Python中使用多处理。结果和任何错误消息都可以在列表中返回。在

示例:

假设您有三个音频文件,your_function将并行运行3次,并返回错误消息。在

import subprocess
from multiprocessing import Pool, cpu_count

def multi_processor(function_name):

    # Use a regex to make a list of full paths for audio files in /some/directory
    # You could also just pass in a list of audio files as a parameter to this function
    file_list = []
    file_list = str(subprocess.check_output("find ./some/directory -type f -iname \"*a_string_in_your_aud_file_name*\" ",shell=True)).split('\\n')
    file_list = sorted(file_list)

    # Test, comment out two lines above and put 3 strings in the list so your_function should run three times with 3 processors in parallel
    file_list.append("test1")
    file_list.append("test2")
    file_list.append("test3")

    # Use max number of system processors - 1
    pool = Pool(processes=cpu_count()-1)
    pool.daemon = True

    results = {}
    # for every audio file in the file list, start a new process
    for aud_file in file_list:
        results[aud_file] = pool.apply_async(your_function, args=("arg1", "arg2"))

    # Wait for all processes to finish before proceeding
    pool.close()
    pool.join()

    # Results and any errors are returned
    return {your_function: result.get() for your_function, result in results.items()}


def your_function(arg1, arg2):
    try:
        print("put your stuff in this function")
        your_results = ""
        return your_results
    except Exception as e:
        return str(e)


if __name__ == "__main__":
    multi_processor("your_function")

相关问题 更多 >