The Difference between os.system and subprocess calls

I have created a program that creates a web architecture in a local server then loads the necessary browser to display the html and php pages on localhost.

The os.system call kills the python process but doesn't kill the other processes -- for example, httpd.exe and mysqld.exe

The subprocess call kills the httpd.exe and mysqld.exe programs but continues to run the python code, and no code executes after the subprocess call.

How would i go about killing or hiding all necessary processes after the python code is executed?

Here is my code.

os.makedirs(dr + x + '/admin' + '/css')
dobj = open(dr + x + '/admin' + '/css' + '/style.css', 'w')
dobj.close()
del dobj
os.makedirs(dr + x + '/admin' + '/js')
os.makedirs(dr + x + '/admin' + '/img')
################################################################################
## THE OS SYSTEM CALLS CLOSE THE APP BUT OPEN THE PROCESSES
## AND THE SUBPROCESS CALLS CLOSE BOTH PROCESSES AND LEAVES THE APP OPEN
## CANT WIN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
os.makedirs(dr + x + '/admin' + '/conf')
#os.system(r'C:xamppapachebinhttpd.exe')
#os.system(r'C:xamppmysqlbinmysqld.exe')
subprocess.Popen(['C:xamppapachebinhttpd.exe'], shell=True, creationflags=subprocess.SW_HIDE)
subprocess.Popen(['C:xamppmysqlbinmysqld.exe'], shell=True, creationflags=subprocess.SW_HIDE)
webbrowser.open('localhost/' + x)
sys.exit()
################################################################################


else:
    backmaybe = raw_input('Already Exists... Try Again? (Y/N) ')
if backmaybe == 'y':
    start()
else:
    sys.exit()

The difference between os.system and subprocess.Popen is that Popen actually opens a pipe, and os.system starts a subshell, much like subprocess.call . Windows only half-supports some pipe/shell features of what *nix operating systems will, but the difference should still fundamentally be the same. A subshell doesn't let you communicate with the standard input and output of another process like a pipe does.

What you probably want is to use subprocess like you are, but then call the kill() method (from the docs) on the pipe object before your application terminates. That will let you decide when you want the process terminated. You might need to satisfy whatever i/o the process wants to do by calling pipe.communicate() and closing the pipe's file handles.

链接地址: http://www.djcxy.com/p/11396.html

上一篇: 从一个效果追溯到它在大型Python代码库中的原因

下一篇: os.system和子进程调用之间的区别