代码之家  ›  专栏  ›  技术社区  ›  user915783

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

  •  6
  • user915783  · 技术社区  · 7 年前

    我正在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脚本在ASR云引擎上运行输入音频文件,运行时,我可以在Matlab控制台中看到python音频文件的语音识别分数。 我要执行以下操作:

    (1) 并行执行多个此类命令。比方说,我有2个输入音频文件(每个文件都有不同的音频段),我希望使用单独的进程并行运行上述命令2次。我能够创建一个代码段,该代码段应该能够做到这一点:

     for i=1: 2
         userAuthCode=i;
         cmd = ['C:\Python27\python.exe runASR.py userAuthCode];  
         runtime = java.lang.Runtime.getRuntime();        
         pid(i) = runtime.exec(cmd);
     end
    
     for i=1:2
        pid(i).waitFor();
        % get exit status
        rc(i) = pid(i).exitValue();       
     end
    

    现在,当执行上述代码时,我可以看到data1的ASRE分数,但不能看到data 2的ASRE分数。
    变量中的退出状态 钢筋混凝土 0,1, 这证实了这一点。 问题是我不知道错误的原因,因为没有打印任何内容 Matlab。如何从java/Matlab中捕获的Python中获取错误消息 变量以便我可以查看?

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

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

    谢谢
    塞迪

    1 回复  |  直到 7 年前
        1
  •  1
  •   Calculus    7 年前

    一种方法是在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")