summaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
authorBaruch Sterin <baruchs@gmail.com>2011-02-27 18:33:56 -0800
committerBaruch Sterin <baruchs@gmail.com>2011-02-27 18:33:56 -0800
commit34d59b0b91f09b35b7c3b4e74411f11b8b3994cc (patch)
treea721b34849df3243b145ba62b504ed41ab618dbc /scripts
parent02081dba6779fbd247d1e62d5dc6db2097acc8e7 (diff)
downloadabc-34d59b0b91f09b35b7c3b4e74411f11b8b3994cc.tar.gz
abc-34d59b0b91f09b35b7c3b4e74411f11b8b3994cc.tar.bz2
abc-34d59b0b91f09b35b7c3b4e74411f11b8b3994cc.zip
fixes to pyabc kill mechanism
Diffstat (limited to 'scripts')
-rw-r--r--scripts/reachx_cmd.py50
1 files changed, 30 insertions, 20 deletions
diff --git a/scripts/reachx_cmd.py b/scripts/reachx_cmd.py
index dd59eb0a..0ed2876c 100644
--- a/scripts/reachx_cmd.py
+++ b/scripts/reachx_cmd.py
@@ -12,23 +12,35 @@ from contextlib import contextmanager, nested
import pyabc
-def wait_with_timeout(p, timeout):
+def popen_and_wait_with_timeout(timeout,cmd, *args, **kwargs):
""" Wait for a subprocess.Popen object to terminate, or until timeout (in seconds) expires. """
- if timeout <= 0:
- timeout = None
-
- t = threading.Thread(target=lambda: p.wait())
- t.start()
-
- t.join(timeout)
-
- if t.is_alive():
- p.kill()
-
- t.join()
-
- return p.returncode
+ p = None
+ t = None
+
+ try:
+ p = subprocess.Popen(cmd, *args, **kwargs)
+
+ if timeout <= 0:
+ timeout = None
+
+ t = threading.Thread(target=lambda: p.communicate())
+ t.start()
+
+ t.join(timeout)
+
+ finally:
+
+ if p is not None and p.poll() is None:
+ p.kill()
+
+ if t is not None and t.is_alive():
+ t.join()
+
+ if p is not None:
+ return p.returncode
+
+ return -1
@contextmanager
def replace_sys_argv(argv):
@@ -74,13 +86,11 @@ def run_reachx_cmd(effort, timeout):
'qua_ffix -effort %d -L %s'%(effort, cygpath(tmplog_name)),
'quit'
]
-
+
cmd = ["jabc", "-c", " ; ".join(cmdline)]
- p = subprocess.Popen(cmd, shell=False, stdout=sys.stdout, stderr=sys.stderr)
-
- rc = wait_with_timeout(p,timeout)
-
+ rc = popen_and_wait_with_timeout(timeout, cmd, shell=False, stdout=sys.stdout, stderr=sys.stderr)
+
if rc != 0:
# jabc failed or stopped. Write a status file to update the status to unknown
with open(tmplog_name, "w") as f: