Есть ли способ завершить процесс, запущенный с помощью подпроцесса. Открытый класс с аргументом «shell», установленным в «True»? В рабочем минимальном примере ниже (используется wxPython) вы можете успешно открыть и завершить процесс Notepad, однако, если вы измените аргумент «shell» Popen на «True», процесс Notepad не завершится.Python 2.6 на Windows: как завершить подпроцесс.Popen с аргументом «shell = True»?
import wx
import threading
import subprocess
class MainWindow(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, 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()
Примите, пожалуйста, ради этого вопроса, что «оболочка» должна быть равна «True».
Какая версия Python? –
related: [Как завершить подпроцесс python, запущенный с помощью оболочки = True] (http: // stackoverflow.com/questions/4789837/how-to-terminate-a-python-subprocess-launch-with-shell-true) – jfs