Skip to content

Commit

Permalink
Reimplement run using threads. Django does not like signals (except i…
Browse files Browse the repository at this point in the history
…n main thread).

Ticket:	# 10814
Merge-FN93: yes
Merge-TN93: yes
(cherry picked from commit 8a31ab9)
  • Loading branch information
John Hixson committed Jul 31, 2015
1 parent d791629 commit 9a5a18f
Showing 1 changed file with 35 additions and 25 deletions.
60 changes: 35 additions & 25 deletions gui/common/pipesubr.py
Expand Up @@ -32,6 +32,7 @@
import logging
import os
import signal
import threading


logging.NOTICE = 60
Expand Down Expand Up @@ -78,39 +79,48 @@ def pipeopen(command, important=True, logger=log, allowfork=False, quiet=False):
close_fds=False, preexec_fn=preexec_fn)


def run(command, important=True, logger=log, allowfork=False, quiet=False, timeout=-1):
class run_alarm:
pass
def run_alarm_handler(sig, frame):
raise run_alarm
class Command(object):
def __init__(self, command):
self.command = command
self.process = None
self.stdout = None
self.stderr = None

stdout = stderr = None
@property
def returncode(self):
ret = -1
if self.process:
ret = self.process.returncode
return ret

try:
timeout = long(timeout)
except:
timeout = -1
def run(self, important=True, logger=log, allowfork=False, quiet=False, timeout=None):
def target():
self.process = pipeopen(self.command, important, logger, allowfork, quiet)
(self.stdout, self.stderr) = self.process.communicate()

p = pipeopen(command, important, logger, allowfork, quiet)
if timeout != -1:
signal.signal(signal.SIGALRM, run_alarm_handler)
signal.alarm(timeout)
thread = threading.Thread(target=target)
thread.start()

try:
(stdout, stderr) = p.communicate()
if timeout != -1:
signal.alarm(0)
thread.join(timeout)
if thread.is_alive():
self.process.terminate()
thread.join()

except run_alarm:
try:
os.kill(p.pid, signal.SIGKILL)
return (self.returncode, self.stdout, self.stderr)

except OSError:
pass

return (-9, None, None)
def run(command, important=True, logger=log, allowfork=False, quiet=False, timeout=-1):
try:
timeout = float(timeout)
except:
timeout = 0

if timeout <= 0:
timeout = None

c = Command(command)

return (p.returncode, stdout, stderr)
return c.run(important, logger, allowfork, quiet, timeout)


def system(command, important=True, logger=log):
Expand Down

0 comments on commit 9a5a18f

Please sign in to comment.