Browse Source

Watch for process termination too

tags/nilmtools-1.4.0^0
Jim Paris 10 years ago
parent
commit
87a26c907b
1 changed files with 20 additions and 5 deletions
  1. +20
    -5
      nilmtools/pipewatch.py

+ 20
- 5
nilmtools/pipewatch.py View File

@@ -52,19 +52,28 @@ def reader_thread(queue, fd):
while True:
(r, w, x) = select.select([fd], [], [fd], 0.25)
if x:
raise Exception
raise Exception # generator died?
if not r:
# short timeout -- just try again. This is to catch the
# fd being closed elsewhere, which is only detected
# when select restarts.
continue
data = os.read(fd, 65536)
if data == "":
if data == "": # generator EOF
raise Exception
queue.put(data)
except Exception:
queue.put(None)

def watcher_thread(queue, procs):
# Put None in the queue if either process dies
while True:
for p in procs:
if p.poll() is not None:
queue.put(None)
return
time.sleep(0.25)

def main(argv = None):
args = parse_args(argv)

@@ -90,6 +99,9 @@ def main(argv = None):
reader = threading.Thread(target = reader_thread,
args = (queue, generator.stdout.fileno()))
reader.start()
watcher = threading.Thread(target = watcher_thread,
args = (queue, [generator, consumer]))
watcher.start()
try:
while True:
try:
@@ -116,10 +128,13 @@ def main(argv = None):
break
time.sleep(0.1)
return proc.poll()
if poll_timeout(proc, 0.5) is None:
proc.terminate()
try:
if poll_timeout(proc, 0.5) is None:
proc.kill()
proc.terminate()
if poll_timeout(proc, 0.5) is None:
proc.kill()
except OSError:
pass
return poll_timeout(proc, 0.5)

# Wait for them to die, or kill them


Loading…
Cancel
Save