Commit 037f4e4e authored by Denis Bilenko's avatar Denis Bilenko

greentest/mysubprocess.py: add interrupt() method, make send_signal() not...

greentest/mysubprocess.py: add interrupt() method, make send_signal() not raise ValueError if signal not available and use taskkill on windows if otherwise killing does not work
parent efe644cb
import sys import sys
import os
import subprocess import subprocess
import signal
from subprocess import * from subprocess import *
class Popen(subprocess.Popen): class Popen(subprocess.Popen):
def send_signal(self, sig):
if sys.platform == 'win32':
sig = signal.SIGTERM
if hasattr(subprocess.Popen, 'send_signal'):
try:
return subprocess.Popen.send_signal(self, sig)
except Exception:
sys.stderr.write('send_signal(%s, %s) failed: %s\n' % (self.pid, sig, ex))
self.external_kill()
else:
if hasattr(os, 'kill'):
sys.stderr.write('Sending signal %s to %s\n' % (sig, self.pid))
try:
os.kill(self.pid, sig)
except Exception, ex:
sys.stderr.write('Error while killing %s: %s\n' % (self.pid, ex))
self.external_kill()
else:
self.external_kill()
if not hasattr(subprocess.Popen, 'kill'): if not hasattr(subprocess.Popen, 'kill'):
def kill(self): def kill(self):
try: return self.send_signal(getattr(signal, 'SIGTERM', 15))
from os import kill
sys.stderr.write('Sending signal 9 to %s\n' % self.pid) if not hasattr(subprocess.Popen, 'terminate'):
kill(self.pid, 9)
except ImportError: def terminate(self):
return self.send_signal(getattr(signal, 'SIGTERM', 9))
def interrupt(self):
sig = getattr(signal, 'SIGINT', 2)
return self.send_signal(sig)
def external_kill(self):
if sys.platform == 'win32':
sys.stderr.write('Killing %s: %s\n' % (self.pid, ex))
os.system('taskkill /f /pid %s' % self.pid)
else:
sys.stderr.write('Cannot kill on this platform. Please kill %s\n' % self.pid) sys.stderr.write('Cannot kill on this platform. Please kill %s\n' % self.pid)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment