Python,无法在windows中使用Ctrl-C中断子进程

Python,无法在windows中使用Ctrl-C中断子进程,python,subprocess,copy-paste,interrupt,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"

我有一些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()

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

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()

感谢@J.F.Sebastian,我没有使用键盘中断,而是将按键(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()

另一种方法是为Ctrl+C:
signal.signal(signal.SIGINT,lambda*a:x.KillProcess())
(在主线程中)设置自定义处理程序。@sarbjit非常感谢您的帮助。不知怎么的,这对我的案子不起作用。我调用了外部可执行程序,但控制台是隐藏的,我通过管道将控制台的输出打印到GUI。随着time.sleep(0.5),主线程被阻塞,我再也看不到输出了。我需要GUI在打印可执行程序的输出时仍能工作。@J.F.Sebastian谢谢!如果显示我的外部可执行程序的控制台窗口,Ctrl+C可以工作,但我需要隐藏控制台窗口,这就是为什么它不能接收SIGINT。@Y.Yuan:如果没有控制台(在Ctrl+C上生成SIGINT);您应该使用GUI工具包提供的任何方式显式绑定密钥,