如何通过PID或句柄向子进程发送“Ctrl+Break”

3 投票
2 回答
2700 浏览
提问于 2025-04-17 00:19
import subprocess

proc = subprocess.Popen(['c:\windows\system32\ping.exe','127.0.0.1', '-t'],stdout=subprocess.PIPE) 
while True: 
  line = proc.stdout.readline() 
  print "ping result:", line.rstrip() 
  #sendkey("Ctrl+Break", proc)            # i need this here, this is not for terminate the process but to print a statistics result for the ping result.

如果有人知道怎么做,请告诉我,谢谢!

2 个回答

2

Ctrl+Break键组合会发送一个叫做SIGBREAK的信号。

在Linux系统中,你可以用kill命令来发送这个信号,而在Windows系统中,稍微有点不同。你可以使用一个叫做SendSignal的工具来实现。

2

Windows系统?试试这个:

import signal
proc.send_signal(signal.SIGBREAK)

如果你是指信号中断(kill -2

import signal
proc.send_signal(signal.SIGINT)

撰写回答