| # Gaudi Framework, version v22r0 |

Home |  | Generated: 9 Feb 2011

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

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

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

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

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

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

Definition at line 831 of file GaudiTest.py.

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

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

## Member Data Documentation

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

Definition at line 821 of file GaudiTest.py.

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

Definition at line 887 of file GaudiTest.py.

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

Definition at line 945 of file GaudiTest.py.

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

Definition at line 822 of file GaudiTest.py.

GaudiTest::GaudiFilterExecutable::stack_trace_file

Definition at line 823 of file GaudiTest.py.

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

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

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines