| # Gaudi Framework, version v21r7 |

Home |  | Generated: 22 Jan 2010

# GaudiTest::GaudiFilterExecutable Class Reference

List of all members.

## Public Member Functions
def | __init__
## Public Attributes
| stack_trace_file
## Private Member Functions
def | __UseSeparateProcessGroupForChild
def | _HandleChild
| Needs to replace the ones from RedirectedExecutable and TimeoutExecutable.
def | __Monitor
## Private Attributes
| __input
| __timeout
| __monitor_pid
| __monitor_thread

## Detailed Description

Definition at line 808 of file GaudiTest.py.

## Member Function Documentation

def GaudiTest::GaudiFilterExecutable::__init__ | ( |  | self,
|  |  | input,
|  |  | timeout = `-1` |
| ) |  |  |

    Create a new 'Filter'.

    'input' -- The string containing the input to provide to the
    child process.

    'timeout' -- As for 'TimeoutExecutable.__init__'.

Definition at line 809 of file GaudiTest.py.

    00809                                            :
    00810         """Create a new 'Filter'.
    00811
    00812         'input' -- The string containing the input to provide to the
    00813         child process.
    00814
    00815         'timeout' -- As for 'TimeoutExecutable.__init__'."""
    00816
    00817         super(GaudiFilterExecutable, self).__init__(input, timeout)
    00818         self.__input = input
    00819         self.__timeout = timeout
    00820         self.stack_trace_file = None
    00821         # Temporary file to pass the stack trace from one process to the other
    00822         # The file must be closed and reopened when needed to avoid conflicts
    00823         # between the processes
    00824         tmpf = tempfile.mkstemp()
    00825         os.close(tmpf[0])
    00826         self.stack_trace_file = tmpf[1] # remember only the name
    00827
        def __UseSeparateProcessGroupForChild(self):

def GaudiTest::GaudiFilterExecutable::__UseSeparateProcessGroupForChild | ( |  | self | ) | `[private]`

    Copied from TimeoutExecutable to allow the re-implementation of
       _HandleChild.

Definition at line 828 of file GaudiTest.py.

    00828                                                :
    00829         """Copied from TimeoutExecutable to allow the re-implementation of
    00830            _HandleChild.
    00831         """
    00832         if sys.platform == "win32":
    00833             # In Windows 2000 (or later), we should use "jobs" by
    00834             # analogy with UNIX process groups.  However, that
    00835             # functionality is not (yet) provided by the Python Win32
    00836             # extensions.
    00837             return 0
    00838
    00839         return self.__timeout >= 0 or self.__timeout == -2
        ##

def GaudiTest::GaudiFilterExecutable::_HandleChild | ( |  | self | ) | `[private]`

Needs to replace the ones from RedirectedExecutable and TimeoutExecutable.

    Code copied from both FilterExecutable and TimeoutExecutable.

Definition at line 842 of file GaudiTest.py.

    00842                           :
    00843         """Code copied from both FilterExecutable and TimeoutExecutable.
    00844         """
    00845         # Close the pipe ends that we do not need.
    00846         if self._stdin_pipe:
    00847             self._ClosePipeEnd(self._stdin_pipe[0])
    00848         if self._stdout_pipe:
    00849             self._ClosePipeEnd(self._stdout_pipe[1])
    00850         if self._stderr_pipe:
    00851             self._ClosePipeEnd(self._stderr_pipe[1])
    00852
    00853         # The pipes created by 'RedirectedExecutable' must be closed
    00854         # before the monitor process (created by 'TimeoutExecutable')
    00855         # is created.  Otherwise, if the child process dies, 'select'
    00856         # in the parent will not return if the monitor process may
    00857         # still have one of the file descriptors open.
    00858
    00859         super(qm.executable.TimeoutExecutable, self)._HandleChild()
    00860
    00861         if self.__UseSeparateProcessGroupForChild():
    00862             # Put the child into its own process group.  This step is
    00863             # performed in both the parent and the child; therefore both
    00864             # processes can safely assume that the creation of the process
    00865             # group has taken place.
    00866             child_pid = self._GetChildPID()
    00867             try:
    00868                 os.setpgid(child_pid, child_pid)
    00869             except:
    00870                 # The call to setpgid may fail if the child has exited,
    00871                 # or has already called 'exec'.  In that case, we are
    00872                 # guaranteed that the child has already put itself in the
    00873                 # desired process group.
    00874                 pass
    00875             # Create the monitoring process.
    00876             #
    00877             # If the monitoring process is in parent's process group and
    00878             # kills the child after waitpid has returned in the parent, we
    00879             # may end up trying to kill a process group other than the one
    00880             # that we intend to kill.  Therefore, we put the monitoring
    00881             # process in the same process group as the child; that ensures
    00882             # that the process group will persist until the monitoring
    00883             # process kills it.
    00884             self.__monitor_pid = os.fork()
    00885             if self.__monitor_pid != 0:
    00886                 # Make sure that the monitoring process is placed into the
    00887                 # child's process group before the parent process calls
    00888                 # 'waitpid'.  In this way, we are guaranteed that the process
    00889                 # group as the child
    00890                 os.setpgid(self.__monitor_pid, child_pid)
    00891             else:
    00892                 # Put the monitoring process into the child's process
    00893                 # group.  We know the process group still exists at
    00894                 # this point because either (a) we are in the process
    00895                 # group, or (b) the parent has not yet called waitpid.
    00896                 os.setpgid(0, child_pid)
    00897
    00898                 # Close all open file descriptors.  They are not needed
    00899                 # in the monitor process.  Furthermore, when the parent
    00900                 # closes the write end of the stdin pipe to the child,
    00901                 # we do not want the pipe to remain open; leaving the
    00902                 # pipe open in the monitor process might cause the child
    00903                 # to block waiting for additional input.
    00904                 try:
    00905                     max_fds = os.sysconf("SC_OPEN_MAX")
    00906                 except:
    00907                     max_fds = 256
    00908                 for fd in xrange(max_fds):
    00909                     try:
    00910                         os.close(fd)
    00911                     except:
    00912                         pass
    00913                 try:
    00914                     if self.__timeout >= 0:
    00915                         # Give the child time to run.
    00916                         time.sleep (self.__timeout)
    00917                         #######################################################
    00918                         ### This is the interesting part: dump the stack trace to a file
    00919                         if sys.platform == "linux2": # we should be have /proc and gdb
    00920                             cmd = ["gdb",
    00921                                    os.path.join("/proc", str(child_pid), "exe"),
    00922                                    str(child_pid),
    00923                                    "-batch", "-n", "-x",
    00924                                    "'%s'" % os.path.join(os.path.dirname(__file__), "stack-trace.gdb")]
    00925                             # FIXME: I wanted to use subprocess.Popen, but it doesn't want to work
    00926                             #        in this context.
    00927                             o = os.popen(" ".join(cmd)).read()
    00928                             open(self.stack_trace_file,"w").write(o)
    00929                         #######################################################
    00930
    00931                         # Kill all processes in the child process group.
    00932                         os.kill(0, signal.SIGKILL)
    00933                     else:
    00934                         # This call to select will never terminate.
    00935                         select.select ([], [], [])
    00936                 finally:
    00937                     # Exit.  This code is in a finally clause so that
    00938                     # we are guaranteed to get here no matter what.
    00939                     os._exit(0)
    00940         elif self.__timeout >= 0 and sys.platform == "win32":
    00941             # Create a monitoring thread.
    00942             self.__monitor_thread = Thread(target = self.__Monitor)
    00943             self.__monitor_thread.start()
    00944
        if sys.platform == "win32":

def GaudiTest::GaudiFilterExecutable::__Monitor | ( |  | self | ) | `[private]`

    Code copied from FilterExecutable.
    Kill the child if the timeout expires.

    This function is run in the monitoring thread.

Definition at line 947 of file GaudiTest.py.

    00947                            :
    00948             """Code copied from FilterExecutable.
    00949             Kill the child if the timeout expires.
    00950
    00951             This function is run in the monitoring thread."""
    00952
    00953             # The timeout may be expressed as a floating-point value
    00954             # on UNIX, but it must be an integer number of
    00955             # milliseconds when passed to WaitForSingleObject.
    00956             timeout = int(self.__timeout * 1000)
    00957             # Wait for the child process to terminate or for the
    00958             # timer to expire.
    00959             result = win32event.WaitForSingleObject(self._GetChildPID(),
    00960                                                     timeout)
    00961             # If the timeout occurred, kill the child process.
    00962             if result == win32con.WAIT_TIMEOUT:
    00963                 self.Kill()
    00964
    ########################################################################

## Member Data Documentation

GaudiTest::GaudiFilterExecutable::__input`[private]`

Definition at line 818 of file GaudiTest.py.

GaudiTest::GaudiFilterExecutable::__timeout`[private]`

Definition at line 819 of file GaudiTest.py.

GaudiTest::GaudiFilterExecutable::stack_trace_file

Definition at line 820 of file GaudiTest.py.

GaudiTest::GaudiFilterExecutable::__monitor_pid`[private]`

Definition at line 884 of file GaudiTest.py.

GaudiTest::GaudiFilterExecutable::__monitor_thread`[private]`

Definition at line 942 of file GaudiTest.py.

The documentation for this class was generated from the following file:

- /afs/cern.ch/sw/Gaudi/releases/GAUDI/GAUDI_v21r7/GaudiPolicy/qmtest_classes/GaudiTest.py