python - How to achieve desired results when using the subprocees Popen.send_signal(CTRL_C_EVENT) in Windows? -


in python 2.7 in windows according documentation can send ctrl_c_event (python 2.7 subprocess popen.send_signal documentation). when tried did not receive expected keyboard interrupt in subprocess.

this sample code for 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 1 message every 0.5 seconds     time.sleep(0.5)     # send data subprocess     process.stdin.write('bar\n')     # read data subprocess     temp = process.stdout.readline()     print temp,     if (index == 10):         # send keyboard interrupt         process.send_signal(signal.ctrl_c_event) 

this sample code child proceess:

# file : childprocess.py import sys  while true:     try:         # data main process         temp = sys.stdin.readline()         # write data out         print 'foo ' + temp,     except keyboardinterrupt:         print "keyboardinterrupt" 

if run file parentprocess.py expect "foo bar" ten times "keyboardinterrupt" followed "foo bar" 4 times "foo bar" 15 times instead.

is there way ctrl_c_event behave keyboard interrupt sigint behaves in linux?

after doing reading found information seems contradic python documentation regarding ctrl_c_event, in particular says

ctrl_c_event 0 generates ctrl+c signal. signal cannot generated process groups

the following site provide more inforamtion creation flags: process creation flags.

this method of signal handling subprocesses worked me on both linux , windows 2008, both using python 2.7.2, uses ctrl-break instead of ctrl-c. see note process groups , ctrl-c in http://msdn.microsoft.com/en-us/library/ms683155%28v=vs.85%29.aspx.

catcher.py:

import os import signal import sys import time  def signal_handler(signal, frame):   print 'catcher: signal %d received!' % signal   raise exception('catcher: 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 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 couple of seconds catcher start...' time.sleep(2) print 'thrower: sending signal catch'  if hasattr(os.sys, 'winver'):     os.kill(process.pid, signal.ctrl_break_event) else:     process.send_signal(signal.sigterm)  print 'thrower: done' 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -