1

I am trying to execute a script using multiprocessing. But in case of errors the script is not throwing exception and pulling out of the processing. Is it possible to have an exception thrown and come out of the execution. I am not able to figure out how to do this since I am new to python.

Below is the script I have written :

import os 
import sys
import string
import multiprocessing as mp

path=sys.argv[1]

path_tpt=path+'/tpt_script'


""" Multiprocessing module to generate intermediate exports"""



total_tpt_file_list = []
for filename in os.listdir(path_tpt):
    total_tpt_file_list.append(os.path.join(path_tpt,filename))

total_tpt_file_list = list(filter(lambda x:x.endswith('.tpt'), total_tpt_file_list))
total_tpt_file_list = sorted(total_tpt_file_list)
print(total_tpt_file_list) 



def run_sed (file_list):
    x=file_list.rsplit("/", 2)
    y=x[0]
    file_name=x[2]
    print(file_name)
    path_ctl=str(y)+'/ctl_file'
    path_tpt=str(y)+'/tpt_script'
    path_log=str(y)+'/log'
    print("tbuild -f  "+ (file_list) + " -j  "+ 'tpt_chk_$$ '+ '> '+ os.path.join(path_log,file_name).split('.')[0]+'.log 2>>'+ os.path.join(path_log,file_name).split('.')[0]+'.log')
    status=os.system("tbuild -f  "+ (file_list) + " -j  "+ 'tpt_chk_$$ '+ '> '+ os.path.join(path_log,file_name).split('.')[0]+'.log 2>>'+ os.path.join(path_log,file_name).split('.')[0]+'.log')

try:    
    p = mp.Pool(processes=(mp.cpu_count()-1))
    total_file_list = p.map(run_sed,total_tpt_file_list)

finally:
    p.close()
    p.join()
    print("done")

Please let me know if anymore information is needed.

Thanks in Advance.

1
  • Where and what kind of exceptions are thrown in your script that you want to handle? Please be aware that we cannot run your script, and therefore don’t know how it and its auxiliary processes behave. Commented Feb 26, 2020 at 8:00

1 Answer 1

1

Python exceptions will be propagated back up, given what I see, I'm guessing you're wanting errors from your os.system call to be raised. You'd need to do something like

if status != 0: 
    raise Exception("eeek")

What I really recommend, rather than using os.system is to use subprocess.check_output https://docs.python.org/3/library/subprocess.html#subprocess.check_output It will automatically check the status code of the subprocess call and raise any exception with the status code and the output text. So, your code would look something like:

    subprocess.check_output(["tbuild", "-f"] + file_list + ["-j", "tpt_chk_$$", ...

You may also want to return the output (return subprocess.check_output...)

Sign up to request clarification or add additional context in comments.

4 Comments

Will the script exit from execution as well?
What do you mean by "script"? If a call to tbuild fails, it'll be what raises the exception so it'll already have stopped. If you don't catch the exception your python program will stop. Other subprocess calls to tbuild should exit when the python program stops I think.
I am trying to run a multiprocessing script. I am just concerned , if exiting from the main process will also ensure that the child processes es are also exit-ed
In my testing, the other processes did not stop. That's a whole separate problem if you want to solve that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.