2011-08-25 70 views
2
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. 

如果有人知道该怎么做,请与我分享,谢谢!如何通过pid或处理程序将“Ctrl + Break”发送到子进程

+0

发送过程中的信号,而不是钥匙。 – Keith

+0

creationflags = subprocess.CREATE_NEW_PROCESS_GROUP应在调用子进程时添加。 – user478514

回答

1

Windows?试试这个:

import signal 
proc.send_signal(signal.SIGBREAK) 

如果你的意思是一个信号中断(kill -2

import signal 
proc.send_signal(signal.SIGINT) 
+0

太棒了,我没有注意到Popen对象上的send_signal;) –

+0

谢谢,我还发现os.kill(proc.pid,signal.CTRL_BREAK_EVENT)在Windows – user478514

2

Ctrl + Break键是一个SIGBREAK信号。

在linux下,你可以用kill这个命令发送这个信号,在Windows上这个稍有不同。您可以使用SendSignal工具。