1.1 --- a/parallel.py Mon Sep 26 22:27:07 2005 +0000
1.2 +++ b/parallel.py Tue Sep 27 14:16:48 2005 +0000
1.3 @@ -68,12 +68,21 @@
1.4 timeout value as an argument to the ready method (as a floating point number
1.5 specifying the timeout in seconds, where 0 means a non-blocking poll as stated
1.6 in the select module's select function documentation).
1.7 +
1.8 +Signals and Waiting
1.9 +-------------------
1.10 +
1.11 +When created/child processes terminate, one would typically want to be informed
1.12 +of such conditions using a signal handler. Unfortunately, Python seems to have
1.13 +issues with restartable reads from file descriptors when interrupted by signals:
1.14 +
1.15 +http://mail.python.org/pipermail/python-dev/2002-September/028572.html
1.16 """
1.17
1.18 import os
1.19 import sys
1.20 import select
1.21 -from signal import signal, SIGCHLD
1.22 +#from signal import signal, SIGCHLD
1.23
1.24 try:
1.25 import cPickle as pickle
1.26 @@ -95,14 +104,25 @@
1.27 self.pid = pid
1.28 self.read_pipe = read_pipe
1.29 self.write_pipe = write_pipe
1.30 + self.closed = 0
1.31
1.32 def __del__(self):
1.33
1.34 # NOTE: Hack until the signals vs. pipes behaviour is fixed.
1.35
1.36 + if not self.closed:
1.37 + self.close()
1.38 +
1.39 + def close(self):
1.40 +
1.41 + "Explicitly close the channel."
1.42 +
1.43 + self.closed = 1
1.44 + self.read_pipe.close()
1.45 + self.write_pipe.close()
1.46 if self.pid != 0:
1.47 try:
1.48 - os.wait()
1.49 + os.waitpid(self.pid, os.WNOHANG)
1.50 except OSError:
1.51 pass
1.52
1.53 @@ -138,6 +158,12 @@
1.54 for channel in channels:
1.55 self.readables[channel.read_pipe] = channel
1.56
1.57 + def active(self):
1.58 +
1.59 + "Return the number of active channels."
1.60 +
1.61 + return len(self.readables.keys())
1.62 +
1.63 def ready(self, timeout=None):
1.64
1.65 """
1.66 @@ -158,6 +184,14 @@
1.67 readable = [self.readables[fd] for fd in readable_fds]
1.68 return readable
1.69
1.70 + def remove(self, channel):
1.71 +
1.72 + """
1.73 + Remove the given 'channel' from the exchange.
1.74 + """
1.75 +
1.76 + del self.readables[channel.read_pipe]
1.77 +
1.78 def create():
1.79
1.80 """