Skip to content

Instantly share code, notes, and snippets.

@andre-merzky
Created August 13, 2015 23:36
Show Gist options
  • Save andre-merzky/08b994dd35ef3cf563b6 to your computer and use it in GitHub Desktop.
Save andre-merzky/08b994dd35ef3cf563b6 to your computer and use it in GitHub Desktop.
#!/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()
print
b = B()
b.start()
b.join()
print
c = C()
c.start()
c.join()
print
d = D()
d.start()
d.join()
print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment