how to send a “Ctrl+Break” to a subprocess via pid or handler

爱⌒轻易说出口 提交于 2021-02-08 06:21:38

问题


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.

If someone know how to do it, please share with me, thanks!


回答1:


Windows? Try this:

import signal
proc.send_signal(signal.SIGBREAK)

If you meant a signal interrupt (kill -2)

import signal
proc.send_signal(signal.SIGINT)



回答2:


The Ctrl+Break keys is a SIGBREAK signal.

Under linux, you can send this signal with kill command, on Windows, this is slightly different. You can use the SendSignal tool.



来源:https://stackoverflow.com/questions/7186543/how-to-send-a-ctrlbreak-to-a-subprocess-via-pid-or-handler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!