Join a thread instead of sleeping.

* Instead of having a fixed sleep duration, start a thread
that monitors the subprocess.
* Started thread is joined, if process finishes thread returns
before timeout.
* Should increase speed and reduce overhead on CPU.
This commit is contained in:
Jeremy Pallats/starcraft.man 2015-09-17 19:16:39 -04:00
parent 86e75e5d08
commit 91fe6ad3e9

View File

@ -1182,6 +1182,17 @@ class Command(object):
self.proc = subprocess.Popen(self.cmd, cwd=self.cmd_dir, stdout=tfile, self.proc = subprocess.Popen(self.cmd, cwd=self.cmd_dir, stdout=tfile,
stderr=subprocess.STDOUT, shell=True, stderr=subprocess.STDOUT, shell=True,
preexec_fn=os.setsid) preexec_fn=os.setsid)
thrd = thr.Thread(target=(lambda proc: proc.wait()), args=(self.proc,))
thrd.start()
thread_not_started = True
while thread_not_started:
try:
thrd.join(0.1)
thread_not_started = False
except RuntimeError:
pass
while self.alive: while self.alive:
if G_STOP.is_set(): if G_STOP.is_set():
raise KeyboardInterrupt raise KeyboardInterrupt
@ -1196,7 +1207,7 @@ class Command(object):
if time_diff > self.timeout: if time_diff > self.timeout:
raise CmdTimedOut(['Timeout!']) raise CmdTimedOut(['Timeout!'])
time.sleep(0.33) thrd.join(0.5)
tfile.seek(0) tfile.seek(0)
result = [line.decode('utf-8', 'replace').rstrip() for line in tfile] result = [line.decode('utf-8', 'replace').rstrip() for line in tfile]