Wednesday, September 16, 2009

Python - run and manage a subprocess

 
 
# run and or manage a subprocess with the python subprocess module 
def openProcess(pname):
    import subprocess
    exe = subprocess.Popen(pname)
    #print exe.pid 
    exe.poll() # poll() will set returncode 
    if exe.returncode is None:
        print 'process still running' 
        exe.wait() # wait() will also update returncode 
    # at this point the script will wait until the executable quits 
    if exe.returncode is None:
        print 'process still running' 
    else:
        print 'process execution is complete' 
 
if __name__ == '__main__':
    openProcess('notepad.exe')
    
 
##outputs: 
#process still running 
#process execution is complete 
 

No comments:

Post a Comment