7

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

这是父进程的示例代码:

# 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\n')
    # Read data from the subprocess
    temp = process.stdout.readline()
    print temp,
    if (index == 10):
        # Send Keyboard Interrupt
        process.send_signal(signal.CTRL_C_EVENT)

这是子进程的示例代码:

# 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 次,但我得到“Foo Bar”15 次。

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

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

CTRL_C_EVENT 0 生成 CTRL+C 信号。无法为进程组生成此信号

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

4

2 回答 2

7

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

捕手.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)

投掷者.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'
于 2012-05-06T20:55:48.010 回答
0

尝试

win32api.GenerateConsoleCtrlEvent(CTRL_C_EVENT, pgroupid)

或者

win32api.GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, pgroupid)

参考:

http://docs.activestate.com/activepython/2.5/pywin3/win32process_CREATE_NEW_PROCESS_GROUP.html

http://msdn.microsoft.com/en-us/library/ms683155%28v=vs.85%29.aspx

读取关于 dwProcessGroupId 的信息,groupid 应该与进程 id 相同

于 2011-05-22T03:03:03.147 回答