Difference between os.system and subprocess calls

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

Calling os.system kills the python process, but does not kill other processes - for example, httpd.exe and mysqld.exe

A subprocess call kills the httpd.exe and mysqld.exe , but continues to run python code, and after the subprocess call the code fails.

How can I kill or hide all the necessary processes after python code execution?

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:\\xampp\\apache\\bin\\httpd.exe') #os.system(r'C:\\xampp\\mysql\\bin\\mysqld.exe') subprocess.Popen(['C:\\xampp\\apache\\bin\\httpd.exe'], shell=True, creationflags=subprocess.SW_HIDE) subprocess.Popen(['C:\\xampp\\mysql\\bin\\mysqld.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() 
+7
source share
1 answer

The difference between os.system and subprocess.Popen is that Popen actually opens the pipe , and os.system launches a subshell , very similar to subprocess.call . Windows only partially supports some pipe / shell functions from which * nix operating systems will work, but the difference will still be essentially the same. A sub-sell does not allow you to communicate with the standard input and output of another process, such as a channel.

What you probably want is to use a subprocess like you do, but then call the kill () method ( from the documents ) on the handset before your application terminates. This will allow you to decide when you want the process to be interrupted. You may need to perform any operations that the process wants to perform by calling pipe.communicate() and closing the protocol file descriptors.

+3
source

All Articles