在Windows中使用子程序Popen.send_signal(CTRL_C_EVENT)时如何获
发布时间:2020-12-13 20:43:47 所属栏目:Windows 来源:网络整理
导读:在根据文档的Windows中的python 2.7中,您可以发送CTRL_C_EVENT (Python 2.7 Subprocess Popen.send_signal documentation). 但是,当我尝试它时,我没有在子进程中收到预期的键盘中断. 这是父进程的示例代码: # FILE : parentProcess.pyimport subprocessimpo
在根据文档的Windows中的python 2.7中,您可以发送CTRL_C_EVENT
(Python 2.7 Subprocess Popen.send_signal documentation). 但是,当我尝试它时,我没有在子进程中收到预期的键盘中断. 这是父进程的示例代码: # 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('Barn') # 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文档相矛盾,特别是它说
以下站点提供了有关创建标志的更多信息:
这个子进程信号处理方法在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的说明.
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: 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 Server 2016-Hyper-V HNV 新增功能
- win7运行mapreduce报错Could not locate executable null&#
- winforms – Windows窗体中BackgroundImage的位置
- windows – System.IO.File.ReadAllText的默认编码是什么
- windows – 将一个字节的数据附加到文件需要多少次磁盘搜索
- 如何确定接口是否是Windows PC上的物理LAN端口?
- windows – 远程运行调用“以管理员身份运行”的脚本
- windows – 所有操作系统的通用换行符是什么? (LF和CR)
- windows下安装tesserocr
- 什么是* Mlick * UI库/框架/工具,非MFC,Win32 C应用程序?
推荐文章
站长推荐
热点阅读