summaryrefslogtreecommitdiffstats
path: root/src/python/pyabc_split.py
diff options
context:
space:
mode:
authorBaruch Sterin <baruchs@gmail.com>2011-02-01 11:13:53 -0800
committerBaruch Sterin <baruchs@gmail.com>2011-02-01 11:13:53 -0800
commitb538a5fad096764a686a68f843f74ee36d3c7ef1 (patch)
treebdeacf8e2427a12ecb5e047bf865c44b6ffc003a /src/python/pyabc_split.py
parent624af674a0e7f1a675917afaaf371db6a5588821 (diff)
downloadabc-b538a5fad096764a686a68f843f74ee36d3c7ef1.tar.gz
abc-b538a5fad096764a686a68f843f74ee36d3c7ef1.tar.bz2
abc-b538a5fad096764a686a68f843f74ee36d3c7ef1.zip
1. Replace system() with a function that responds to SIGINT. 2. Add functions to cleanup temporary files on SIGINT. 3. Fix bugs related to signal handling.
Diffstat (limited to 'src/python/pyabc_split.py')
-rw-r--r--src/python/pyabc_split.py29
1 files changed, 27 insertions, 2 deletions
diff --git a/src/python/pyabc_split.py b/src/python/pyabc_split.py
index b52288c2..431a87a8 100644
--- a/src/python/pyabc_split.py
+++ b/src/python/pyabc_split.py
@@ -83,6 +83,7 @@ Author: Baruch Sterin <sterin@berkeley.edu>
"""
import os
+import errno
import sys
import pickle
import signal
@@ -90,6 +91,26 @@ from contextlib import contextmanager
import pyabc
+def _waitpid(pid, flags):
+ while True:
+ try:
+ res = os.waitpid(pid, flags)
+ return res
+ except OSError as e:
+ if e.errno != errno.EINTR:
+ raise
+
+def _wait():
+ while True:
+ try:
+ pid,rc = os.wait()
+ return pid, rc
+ except OSError as e:
+ if e.errno != errno.EINTR:
+ raise
+ except Exceptions as e:
+ raise
+
class _sigint_critical_section(object):
def __init__(self):
self.blocked = False
@@ -132,7 +153,7 @@ class _splitter(object):
with _sigint_critical_section() as cs:
# wait for termination and update result
for pid, _ in self.fds.iteritems():
- os.waitpid( pid, 0 )
+ _waitpid( pid, 0 )
pyabc.remove_child_pid(pid)
self.results[pid] = None
@@ -164,6 +185,7 @@ class _splitter(object):
if pid == 0:
# child process:
+ pyabc.reset_sigint_handler()
cs.release()
os.close(pr)
rc = self.child( pw, f)
@@ -187,9 +209,12 @@ class _splitter(object):
def get_next_result(self):
# wait for the next child process to terminate
- pid, rc = os.wait()
+ pid, rc = _wait()
assert pid in self.fds
+ with _sigint_critical_section() as cs:
+ pyabc.remove_child_pid(pid)
+
# retrieve the pipe file descriptor1
i, fd = self.fds[pid]
del self.fds[pid]