Python,不能在Windows中使用Ctrl-C中断子进程

时间:2016-03-09 06:26:58

标签: python subprocess copy-paste interrupt

我有一些Python代码用子进程调用外部可执行程序,并实时读回输出到GUI,我希望随时用Ctrl-C中断外部二进制文件,但它似乎无法正常工作。

我正在使用Windows。我希望在按下Ctrl-C时停止子进程。

这是我的代码:

class binary_run():
    def __init__ (self, tcl_file_name, cmd_str, output_ctrl, exe_cwd):

        self.some_exe = "E:\\some.exe"
        self.cmd = cmd = self.some_exe + cmd_str
        self.output_ctrl = output_ctrl


    def PrintOutputInRealTime(self):
        #The following two lines are to make sure the console window is hidden
        startupinfo = subprocess.STARTUPINFO()
        startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW

        #Start the subprocess
        process = subprocess.Popen(self.cmd, startupinfo=startupinfo, stdin=subprocess.PIPE, stdout=subprocess.PIPE)

        while True:
            try:
                output = process.stdout.readline()
                if output == '' and process.poll() is not None:
                    break
                if output:
                    self.output_ctrl.write(output)
            except KeyboardInterrupt: #Never comes here
                process.terminate()
        process.terminate()

    def run_binary(self):
        worker = Thread(target=self.PrintOutputInRealTime)
        worker.start()

2 个答案:

答案 0 :(得分:1)

感谢@ J.F.Sebastian,而不是使用KeyboardInterrupt,我将键(Ctrl + Delete)绑定到我的GUI,如果键已关闭,子进程将终止,并且它的工作原理如下:

class binary_run():
    def __init__ (self, tcl_file_name, cmd_str, output_ctrl, exe_cwd):

        self.some_exe = "E:\\some.exe"
        self.cmd = self.some_exe + cmd_str
        self.output_ctrl = output_ctrl

        self.output_ctrl.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)

    def PrintOutputInRealTime(self):
        #The following two lines are to make sure the console window is hidden
        startupinfo = subprocess.STARTUPINFO()
        startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW

        #Start the subprocess
        self.process = subprocess.Popen(self.cmd, startupinfo=startupinfo, stdin=subprocess.PIPE, stdout=subprocess.PIPE)

        while True:
            try:
                #output = process.stdout.readline()
                output = self.process.stdout.readline()
                if output == '' and self.process.poll() is not None:
                    break
                if output:
                    self.output_ctrl.write(output)
            except KeyboardInterrupt:
                self.process.terminate()

        self.process.terminate()

    def OnKeyDown(self, event):
        controlDown = event.ControlDown()
        keycode = event.GetKeyCode()

        if (controlDown and keycode == wx.WXK_DELETE):
            wx.MessageBox('Binary got interrupted!', 'INFO', wx.OK | wx.ICON_INFORMATION)
            self.process.terminate()

    def run_binary(self):
        worker = Thread(target=self.PrintOutputInRealTime)
        worker.start()

答案 1 :(得分:0)

我自己也遇到了类似的情况,我正在使用线程运行可执行文件并读取其数据。我使用以下方法来解决这个问题

import threading
import subprocess
import time

class binary_run():
    def __init__ (self):
        self.some_exe = "notepad.exe"

    def PrintOutputInRealTime(self):
        self.process = subprocess.Popen(self.some_exe,stdin=subprocess.PIPE, stdout=subprocess.PIPE)
        while True:
            try:
                output = self.process.stdout.readline()
                if output == '' and self.process.poll() is not None:
                    break
                if output:
                    self.output_ctrl.write(output)
            except:
                pass

    def KillProcess(self):
        self.process.terminate()

if __name__ == "__main__":
    x = binary_run()
    worker = threading.Thread(target=x.PrintOutputInRealTime)
    worker.start()
    try:
        while worker.isAlive():
            time.sleep(0.5)
    except KeyboardInterrupt:
            print "Got Interrupt"
            x.KillProcess()