| # Gaudi Framework, version v21r8 |

Home |  | Generated: 17 Mar 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 809 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 810 of file GaudiTest.py.

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

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

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

Definition at line 829 of file GaudiTest.py.

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

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

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

## Member Data Documentation

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

Definition at line 819 of file GaudiTest.py.

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

Definition at line 820 of file GaudiTest.py.

GaudiTest::GaudiFilterExecutable::stack_trace_file

Definition at line 821 of file GaudiTest.py.

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

Definition at line 885 of file GaudiTest.py.

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

Definition at line 943 of file GaudiTest.py.

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

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