2011-08-25 66 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工具。