| # Gaudi Framework, version v21r6 |

Home |  | Generated: 11 Nov 2009

# GaudiTest::GaudiFilterExecutable Class Reference

List of all members.

## Detailed Description

Definition at line 807 of file GaudiTest.py.

## 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

## 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 808 of file GaudiTest.py.

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

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

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

Definition at line 827 of file GaudiTest.py.

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

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

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

## Member Data Documentation

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

Definition at line 817 of file GaudiTest.py.

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

Definition at line 818 of file GaudiTest.py.

GaudiTest::GaudiFilterExecutable::stack_trace_file

Definition at line 819 of file GaudiTest.py.

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

Definition at line 883 of file GaudiTest.py.

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

Definition at line 941 of file GaudiTest.py.

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

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