问题描述
我创建了一个程序,它在本地服务器中创建 Web 架构,然后加载必要的浏览器以在 localhost 上显示 html 和 php 页面.
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.
os.system
调用会杀死 python 进程,但不会杀死其他进程——例如,httpd.exe
和 mysqld.exe代码>
The os.system
call kills the python process but doesn't kill the other processes -- for example, httpd.exe
and mysqld.exe
subprocess
调用杀死httpd.exe
和mysqld.exe
程序但继续运行python代码,之后没有代码执行subprocess
调用.
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.
在 python 代码执行后,我将如何杀死或隐藏所有必要的进程?
How would i go about killing or hiding all necessary processes after the python code is executed?
这是我的代码.
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()
推荐答案
os.system
和subprocess.Popen
的区别在于Popen实际上打开了一个pipe,os.system
开始一个subshell,很像 subprocess.call
.Windows 只支持 *nix 操作系统的一些管道/外壳功能,但差异应该基本上是相同的.子shell 不允许您像管道那样与另一个进程的标准输入和输出进行通信.
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.
你可能想要的是像你一样使用子进程,然后调用 kill() 方法(来自文档) 在您的应用程序终止之前在管道对象上.这将让您决定何时终止该进程.您可能需要通过调用 pipe.communicate()
并关闭管道的文件句柄来满足进程想要执行的任何 i/o.
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.
这篇关于os.system 和子进程调用的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!