aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGergely Risko <gergely+context@risko.hu>2009-11-02 17:46:48 +0200
committerGergely Risko <gergely+context@risko.hu>2009-11-02 17:46:48 +0200
commitfcb0ccbd204f0e9eb034cdb17f95f0fbc96672f2 (patch)
tree9b0bdec70bbe9791bdd6da5f655deac5cbf9f61a
parent96cb5159eb019a387191971ab70d343bef5eb19c (diff)
cltool2 theading bugfixes
-rw-r--r--python/ContextKit/cltool2.py33
1 files changed, 20 insertions, 13 deletions
diff --git a/python/ContextKit/cltool2.py b/python/ContextKit/cltool2.py
index 26fc07b6..b418c0a4 100644
--- a/python/ContextKit/cltool2.py
+++ b/python/ContextKit/cltool2.py
@@ -25,8 +25,9 @@ import re
import sys
import time
import math
+import atexit
import signal
-from threading import Thread, Lock
+from threading import Thread, Lock, Event
from subprocess import Popen, PIPE, STDOUT
class Reader(Thread):
@@ -35,20 +36,22 @@ class Reader(Thread):
self.cltool = cltool
self.running = True
- def stop(self):
- self.running = False
-
def run(self):
- while self.running:
+ while True:
l = self.cltool.process.stdout.readline()
if l:
event = (time.time(), CLTool.STDOUT, l)
else:
event = (time.time(), CLTool.COMMENT, "EOF ON STDOUT")
- self.stop()
+ self.running = False
with self.cltool.iolock:
self.cltool.io.append(event)
+ self.cltool.output_notify.set()
+ # not equivalent with a while self.running, this is repeat ... until
+ if not self.running:
+ break
self.cltool.wait()
+ self.cltool = None
class CLTool:
@@ -61,13 +64,14 @@ class CLTool:
preexec_fn=self._preexec)
self.io = []
self.iolock = Lock()
+ self.output_notify = Event()
self.reader = Reader(self)
self.last_expect = 0
- self.reader.setDaemon(True)
self.reader.start()
+ atexit.register(self.atexit)
- def __del__(self):
- self.reader.stop()
+ def atexit(self):
+ self.reader.running = False
self.reader.join()
def _preexec(self):
@@ -88,14 +92,18 @@ class CLTool:
if wantdump:
self.printio()
- def _last_output(self):
+ def _last_output(self, timeout):
"""Compute the output read since the last match."""
+
+ self.output_notify.wait(timeout)
+
r = []
with self.iolock:
for pos in xrange(self.last_expect, len(self.io)):
ts, fno, l = self.io[pos]
if fno == CLTool.STDOUT:
r.append(l)
+ self.output_notify.clear()
return ''.join(r)
# exp: is either a single or a list of regexes and all of them has to match
@@ -117,7 +125,8 @@ class CLTool:
# check if we are matching
# enumerate all of the patterns and remove the matching ones
i = 0
- match_this = self._last_output()
+ now = time.time()
+ match_this = self._last_output(abs_timeout - now)
while i < len(rexp):
if re.search(rexp[i], match_this):
del rexp[i]
@@ -128,7 +137,6 @@ class CLTool:
self.comment('EXPECT OK')
self._return_event(False)
return True
- now = time.time()
# timed out
if now > abs_timeout:
self.comment('TIMEOUT')
@@ -138,7 +146,6 @@ class CLTool:
if not self.reader.running:
self._return_event(wantdump)
return False
- time.sleep(0.1)
def comment(self, st):
"""Append a comment to the io log."""