Python open and kill subprocess -
i'm working on widnows 7 (32 bits) , code:
def start_mviewer_broker(broker_path, test_name): """ function starts broker server""" try: print("**** start_mviewer_broker ****") p = subprocess.popen('start python ' + broker_path + ' ' + test_name, shell=true) return p except: print("**** start_mviewer_broker - exception ****") return 0 def kill_process(p): """ function kills input running process""" try: print("**** kill_process ****") p.terminate() except: print("**** kill_process - exception ****") pass i have few of problems. way launch subprocess shell=true option. if change option false subprocess not launched.
other problem kill process doesn't kill process not exception raised.
any idea?
you want change code following:
def start_mviewer_broker(broker_path, test_name): """ function starts broker server""" try: print("**** start_mviewer_broker ****") return subprocess.popen('python ' + broker_path + ' ' + test_name) # note changed line here except: print("**** start_mviewer_broker - exception ****") return 0 def kill_process(p): """ function kills input running process""" try: print("**** kill_process ****") p.terminate() except: print("**** kill_process - exception ****") pass the start part running not necessary. in fact, not executable cmd command. hence, requires shell run. why wasn't working shell=false. however, upon removing it, can use shell=false. have python process returned process instead of shell used spawn it. killing python process want do, not shell, after removing start part, have kill_process() code working properly.
btw, shell=false default subprocess.popen() , has been left out in above code. also, setting p process , returning seems waste of line of code. can shortened returning directly (as shown in above code).
Comments
Post a Comment