# HG changeset patch # User paulb # Date 1127844243 0 # Node ID 73be19436b6c765c148301596de78c280dd59170 # Parent cadfcdf9a910accc98e657b12d9390f98be2fbd8 [project @ 2005-09-27 18:04:03 by paulb] Fixed pipe creation, closing the other ends of pipes in each process. Made the Channel class's wait method blocking by default. Added some documentation about polling. Switched to sys.exit from SystemExit - this might avoid unnecessary exception handling in created processes. diff -r cadfcdf9a910 -r 73be19436b6c parallel.py --- a/parallel.py Tue Sep 27 17:28:25 2005 +0000 +++ b/parallel.py Tue Sep 27 18:04:03 2005 +0000 @@ -78,6 +78,15 @@ http://mail.python.org/pipermail/python-dev/2002-September/028572.html http://twistedmatrix.com/bugs/issue733 + +Select and Poll +--------------- + +The exact combination of conditions indicating closed pipes remains relatively +obscure. Here is a message/thread describing them (in the context of another +topic): + +http://twistedmatrix.com/pipermail/twisted-python/2005-February/009666.html """ import os @@ -121,15 +130,15 @@ self.closed = 1 self.read_pipe.close() self.write_pipe.close() - self.wait() + self.wait(os.WNOHANG) - def wait(self): + def wait(self, options=0): "Wait for the created process, if any, to exit." if self.pid != 0: try: - os.waitpid(self.pid, os.WNOHANG) + os.waitpid(self.pid, options) except OSError: pass @@ -219,8 +228,12 @@ pid = os.fork() if pid == 0: + os.close(parent_read_fd) + os.close(parent_write_fd) return Channel(pid, os.fdopen(child_read_fd, "r"), os.fdopen(child_write_fd, "w")) else: + os.close(child_read_fd) + os.close(child_write_fd) return Channel(pid, os.fdopen(parent_read_fd, "r"), os.fdopen(parent_write_fd, "w")) def start(callable, *args, **kwargs): @@ -243,7 +256,7 @@ channel.send(exc_value) finally: channel.close() - raise SystemExit + sys.exit(0) else: return channel