Windows上的Python 2.6:如何使用“shell = True”参数终止subpr
发布时间:2020-12-14 04:11:47 所属栏目:Windows 来源:网络整理
导读:有没有办法终止使用subprocess.Popen类启动的进程,并将“ shell”参数设置为“True”?在下面的工作最小示例(使用wx Python)中,您可以愉快地打开和终止记事本进程,但是如果将Popen“shell”参数更改为“True”,则记事本进程不会终止. import wximport thread
有没有办法终止使用subprocess.Popen类启动的进程,并将“
shell”参数设置为“True”?在下面的工作最小示例(使用wx
Python)中,您可以愉快地打开和终止记事本进程,但是如果将Popen“shell”参数更改为“True”,则记事本进程不会终止.
import wx import threading import subprocess class MainWindow(wx.Frame): def __init__(self,parent,id,title): wx.Frame.__init__(self,title) self.main_panel = wx.Panel(self,-1) self.border_sizer = wx.BoxSizer() self.process_button = wx.Button(self.main_panel,-1,"Start process",(50,50)) self.process_button.Bind(wx.EVT_BUTTON,self.processButtonClick) self.border_sizer.Add(self.process_button) self.main_panel.SetSizerAndFit(self.border_sizer) self.Fit() self.Centre() self.Show(True) def processButtonClick(self,event): if self.process_button.GetLabel() == "Start process": self.process_button.SetLabel("End process") self.notepad = threading.Thread(target = self.runProcess) self.notepad.start() else: self.cancel = 1 self.process_button.SetLabel("Start process") def runProcess(self): self.cancel = 0 notepad_process = subprocess.Popen("notepad",shell = False) while notepad_process.poll() == None: # While process has not yet terminated. if self.cancel: notepad_process.terminate() break def main(): app = wx.PySimpleApp() mainView = MainWindow(None,wx.ID_ANY,"test") app.MainLoop() if __name__ == "__main__": main() 请接受这个问题,“shell”必须等于“True”.
你为什么用shell = True?
只是不要这样做.你不需要它,它调用shell,这是没用的. 我不接受它必须是真的,因为它没有.使用shell = True只会给你带来问题并没有任何好处.不惜一切代价避免它.除非你运行一些shell内部命令,否则你不需要它. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- haskell – GHCI在Windows上不那么懒惰?
- windows – 便携式浏览器在部署R Shiny App时出现问题
- windows-8 – Windows 8 WinRT KeyboardCapabilities.Keybo
- 使用Powershell将EBS卷附加到Windows EC2
- windows-8 – Visual Studio Express 2012 for Desktop中没
- windows-xp – 在Windows XP中自动安装软件
- 下载Windows API参考(MSDN)以供离线使用
- windows-server-2012 – 使用远程桌面的最大登录用户数
- Windows Server 2003下配置IIS6.0+php5+MySql5+PHPMyAdmin环
- 在Windows 7上编辑IDLE(Python GUI)上下文菜单