Created
August 13, 2015 23:36
-
-
Save andre-merzky/08b994dd35ef3cf563b6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import time | |
import subprocess | |
import multiprocessing | |
class A(multiprocessing.Process): | |
def __init__(self): | |
multiprocessing.Process.__init__(self) | |
proc = subprocess.Popen(args = '/bin/false') | |
print proc, proc.pid | |
time.sleep(0.01) | |
ec = proc.poll() | |
print 'EXIT CODE: %s' % ec | |
class B(multiprocessing.Process): | |
def __init__(self): | |
multiprocessing.Process.__init__(self) | |
def run(self): | |
proc = subprocess.Popen(args = '/bin/false') | |
print proc, proc.pid | |
time.sleep(0.01) | |
self._watch_queue = multiprocessing.Queue() | |
self._watch_queue.put(proc) | |
self._watcher = multiprocessing.Process(target=self._watch) | |
self._watcher.start () | |
def _watch(self): | |
proc = self._watch_queue.get() | |
print proc, proc.pid | |
ec = proc.poll() | |
print 'exit code: %s' % ec | |
class C(multiprocessing.Process): | |
def __init__(self): | |
multiprocessing.Process.__init__(self) | |
self.proc = None | |
def run(self): | |
self.proc = subprocess.Popen(args = '/bin/false') | |
print self.proc, self.proc.pid | |
time.sleep(0.01) | |
self._watcher = multiprocessing.Process(target=self._watch) | |
self._watcher.start () | |
def _watch(self): | |
print self.proc, self.proc.pid | |
ec = self.proc.poll() | |
print 'exit code: %s' % ec | |
class D(multiprocessing.Process): | |
def __init__(self): | |
multiprocessing.Process.__init__(self) | |
def run(self): | |
proc = subprocess.Popen(args = '/bin/false') | |
print proc, proc.pid | |
time.sleep(0.01) | |
def watch(proc): | |
print proc, proc.pid | |
ec = proc.poll() | |
print 'exit code: %s' % ec | |
self._watcher = multiprocessing.Process(target=watch, args=[proc]) | |
self._watcher.start () | |
a = A() | |
a.start() | |
a.join() | |
b = B() | |
b.start() | |
b.join() | |
c = C() | |
c.start() | |
c.join() | |
d = D() | |
d.start() | |
d.join() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment