Calling multiple instances of python scripts in matlab using java.lang.Runtime.getRuntime not working

I run Matlab2017 on windows 10. I invoke a python script that launches some speech recognition task in the cloud with something like this:

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

When the above command is called, the python script launches the input audio file in the ASR cloud engine, and as it runs, I can see the speech recognition ratings for the audio file with Python in the Matlab console. I want to do the following:

(1) Run several such commands in parallel. Suppose I have 2 input audio files (each of which has different audio segments), and I would like to run the above command 2 times, but in parallel, using separate processes. I managed to create a piece of code that should have done this:

  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 

Now that the above code is executing, I can see the ASRE ratings for data1, but not for data 2.
The exit status in the variable rc is 0.1, which confirms this. The problem is that I do not know the cause of the error, since nothing is printed in Matlab. How can I get an error message from Python written in a java / Matlab variable so that I can take a look?

The problem may be that several ASRE calls in parallel (with different user accounts, of course) may not be supported, but I don’t know if I don’t see the error.

(2) When I run a separate standalone command, as mentioned at the beginning of the post, I can see Score messages for each segment of audio that is printed in the Matlab console, as they are obtained from Python. However, when multiprocessing using java.lang.Runtime.getRuntime () and the associated message code, it does not appear in the Matlab console. Is there a way to display these messages (I assume the display can be asynchronous?)

thanks
Sedy

+7
java python multiprocessing matlab
source share
1 answer

One approach is to use multiprocessing in Python. Results and any error messages may be returned in the list.

Example:

Assuming you have three audio files, your_function will run 3 times in parallel with the returned error messages.

 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") 
+1
source share

All Articles