<tfoot id='tp5ki'></tfoot>
    <bdo id='tp5ki'></bdo><ul id='tp5ki'></ul>

  • <i id='tp5ki'><tr id='tp5ki'><dt id='tp5ki'><q id='tp5ki'><span id='tp5ki'><b id='tp5ki'><form id='tp5ki'><ins id='tp5ki'></ins><ul id='tp5ki'></ul><sub id='tp5ki'></sub></form><legend id='tp5ki'></legend><bdo id='tp5ki'><pre id='tp5ki'><center id='tp5ki'></center></pre></bdo></b><th id='tp5ki'></th></span></q></dt></tr></i><div id='tp5ki'><tfoot id='tp5ki'></tfoot><dl id='tp5ki'><fieldset id='tp5ki'></fieldset></dl></div>
    <legend id='tp5ki'><style id='tp5ki'><dir id='tp5ki'><q id='tp5ki'></q></dir></style></legend>

    1. <small id='tp5ki'></small><noframes id='tp5ki'>

        在 Windows 中使用子进程 Popen.send_signal(CTRL_C_EVENT) 时如何达到预期的效果?

        时间:2023-07-21

        <i id='Uvg3z'><tr id='Uvg3z'><dt id='Uvg3z'><q id='Uvg3z'><span id='Uvg3z'><b id='Uvg3z'><form id='Uvg3z'><ins id='Uvg3z'></ins><ul id='Uvg3z'></ul><sub id='Uvg3z'></sub></form><legend id='Uvg3z'></legend><bdo id='Uvg3z'><pre id='Uvg3z'><center id='Uvg3z'></center></pre></bdo></b><th id='Uvg3z'></th></span></q></dt></tr></i><div id='Uvg3z'><tfoot id='Uvg3z'></tfoot><dl id='Uvg3z'><fieldset id='Uvg3z'></fieldset></dl></div>

            • <bdo id='Uvg3z'></bdo><ul id='Uvg3z'></ul>
                <tfoot id='Uvg3z'></tfoot>

                <small id='Uvg3z'></small><noframes id='Uvg3z'>

                  <tbody id='Uvg3z'></tbody>

                • <legend id='Uvg3z'><style id='Uvg3z'><dir id='Uvg3z'><q id='Uvg3z'></q></dir></style></legend>
                  本文介绍了在 Windows 中使用子进程 Popen.send_signal(CTRL_C_EVENT) 时如何达到预期的效果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  根据文档,在 windows 中的 python 2.7 中,您可以发送 CTRL_C_EVENT(Python 2.7 子进程 Popen.send_signal 文档).但是,当我尝试它时,我没有在子进程中收到预期的键盘中断.

                  In python 2.7 in windows according to the documentation you can send a CTRL_C_EVENT (Python 2.7 Subprocess Popen.send_signal documentation). However when I tried it I did not receive the expected keyboard interrupt in the subprocess.

                  这是父进程的示例代码:

                  This is the sample code for for the parent process:

                  # FILE : parentProcess.py
                  import subprocess
                  import time
                  import signal
                  
                  CREATE_NEW_PROCESS_GROUP = 512
                  process = subprocess.Popen(['python', '-u', 'childProcess.py'],
                                         stdin=subprocess.PIPE,
                                         stdout=subprocess.PIPE,
                                         stderr=subprocess.STDOUT,
                                         universal_newlines=True,
                                         creationflags=CREATE_NEW_PROCESS_GROUP)
                  print "pid = ", process.pid
                  index = 0
                  maxLoops = 15
                  while index < maxLoops:
                      index += 1
                      # Send one message every 0.5 seconds
                      time.sleep(0.5)
                      # Send data to the subprocess
                      process.stdin.write('Bar
                  ')
                      # Read data from the subprocess
                      temp = process.stdout.readline()
                      print temp,
                      if (index == 10):
                          # Send Keyboard Interrupt
                          process.send_signal(signal.CTRL_C_EVENT)
                  

                  这是子进程的示例代码:

                  This is the sample code for the child proceess:

                  # FILE : childProcess.py
                  import sys
                  
                  while True:
                      try:
                          # Get data from main process
                          temp = sys.stdin.readline()
                          # Write data out
                          print 'Foo ' + temp,
                      except KeyboardInterrupt:
                          print "KeyboardInterrupt"
                  

                  如果我运行文件 parentProcess.py,我希望得到Foo Bar"十次,然后是KeyboardInterrupt",然后是Foo Bar"4 次,但我得到了 15 次Foo Bar".

                  If I run the file parentProcess.py I expect to get "Foo Bar" ten times then a "KeyboardInterrupt" followed by "Foo Bar" 4 times but I get "Foo Bar" 15 times instead.

                  有没有办法让 CTRL_C_EVENT 像 SIGINT 在 Linux 中一样充当键盘中断?

                  Is there a way to get the CTRL_C_EVENT to behave as a keyboard interrupt just as SIGINT behaves in Linux?

                  在阅读了一些资料后,我发现了一些似乎与有关 CTRL_C_EVENT 的 python 文档相矛盾的信息,特别是它说

                  After doing some reading I found some information that seems to contradic the python documentation regarding CTRL_C_EVENT, in particular it says that

                  CTRL_C_EVENT0 生成 CTRL+C 信号.无法为进程组生成此信号

                  CTRL_C_EVENT 0 Generates a CTRL+C signal. This signal cannot be generated for process groups

                  以下站点提供了有关创建标志的更多信息:进程创建标志.

                  The following site provide more inforamtion about creation flags: Process Creation Flags.

                  推荐答案

                  这种由子进程处理信号的方法在 Linux 和 Windows 2008 上都适用于我,都使用 Python 2.7.2,但它使用 Ctrl-Break 而不是 Ctrl-C.请参阅 中有关进程组和 Ctrl-C 的说明http://msdn.microsoft.com/en-us/library/ms683155%28v=vs.85%29.aspx.

                  This method of signal handling by subprocesses worked for me on both Linux and Windows 2008, both using Python 2.7.2, but it uses Ctrl-Break instead of Ctrl-C. See the note about process groups and Ctrl-C in http://msdn.microsoft.com/en-us/library/ms683155%28v=vs.85%29.aspx.

                  catcher.py:

                  catcher.py:

                  import os
                  import signal
                  import sys
                  import time
                  
                  def signal_handler(signal, frame):
                    print 'catcher: signal %d received!' % signal
                    raise Exception('catcher: i am done')
                  
                  if hasattr(os.sys, 'winver'):
                      signal.signal(signal.SIGBREAK, signal_handler)
                  else:
                      signal.signal(signal.SIGTERM, signal_handler)
                  
                  print 'catcher: started'
                  try:
                      while(True):
                          print 'catcher: sleeping...'
                          time.sleep(1)
                  except Exception as ex:
                      print ex
                      sys.exit(0)
                  

                  thrower.py:

                  thrower.py:

                  import signal
                  import subprocess
                  import time
                  import os
                  
                  args = [
                      'python',
                      'catcher.py',
                      ]
                  print 'thrower: starting catcher'
                  if hasattr(os.sys, 'winver'):
                      process = subprocess.Popen(args, creationflags=subprocess.CREATE_NEW_PROCESS_GROUP)
                  else:
                      process = subprocess.Popen(args)
                  
                  print 'thrower: waiting a couple of seconds for catcher to start...'
                  time.sleep(2)
                  print 'thrower: sending signal to catch'
                  
                  if hasattr(os.sys, 'winver'):
                      os.kill(process.pid, signal.CTRL_BREAK_EVENT)
                  else:
                      process.send_signal(signal.SIGTERM)
                  
                  print 'thrower: i am done'
                  

                  这篇关于在 Windows 中使用子进程 Popen.send_signal(CTRL_C_EVENT) 时如何达到预期的效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 python 中处理 tcpdump 输出 下一篇:Python 子进程模块比命令慢得多(已弃用)

                  相关文章

                • <legend id='76sa4'><style id='76sa4'><dir id='76sa4'><q id='76sa4'></q></dir></style></legend>

                  • <bdo id='76sa4'></bdo><ul id='76sa4'></ul>
                • <tfoot id='76sa4'></tfoot>

                  1. <i id='76sa4'><tr id='76sa4'><dt id='76sa4'><q id='76sa4'><span id='76sa4'><b id='76sa4'><form id='76sa4'><ins id='76sa4'></ins><ul id='76sa4'></ul><sub id='76sa4'></sub></form><legend id='76sa4'></legend><bdo id='76sa4'><pre id='76sa4'><center id='76sa4'></center></pre></bdo></b><th id='76sa4'></th></span></q></dt></tr></i><div id='76sa4'><tfoot id='76sa4'></tfoot><dl id='76sa4'><fieldset id='76sa4'></fieldset></dl></div>

                  2. <small id='76sa4'></small><noframes id='76sa4'>