Sending ^C to Python subprocess objects on Windows

前端 未结 6 1225
离开以前
离开以前 2020-12-07 19:06

I have a test harness (written in Python) that needs to shut down the program under test (written in C) by sending it ^C. On Unix,

proc.send_sign         


        
6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-07 19:49

    Try calling the GenerateConsoleCtrlEvent function using ctypes. As you are creating a new process group, the process group ID should be the same as the pid. So, something like

    import ctypes
    
    ctypes.windll.kernel32.GenerateConsoleCtrlEvent(0, proc.pid) # 0 => Ctrl-C
    

    should work.

    Update: You're right, I missed that part of the detail. Here's a post which suggests a possible solution, though it's a bit kludgy. More details are in this answer.

提交回复
热议问题