小编Mar*_*rio的帖子

如何在Windows中使用子程序Popen.send_signal(CTRL_C_EVENT)时获得所需的结果?

在根据文档的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):
        # …
Run Code Online (Sandbox Code Playgroud)

python windows subprocess popen keyboardinterrupt

5
推荐指数
2
解决办法
1万
查看次数

标签 统计

keyboardinterrupt ×1

popen ×1

python ×1

subprocess ×1

windows ×1