[Python-Dev] About SSL tests (original) (raw)
Trent Mick trentm at activestate.com
Wed Apr 4 00:31:22 CEST 2007
- Previous message: [Python-Dev] About SSL tests
- Next message: [Python-Dev] About SSL tests
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Christian Heimes wrote:
I'd be willing to look at adding it, if the group thinks it's the right thing to do. I like the idea and I'm proposing to add two more methods to subprocess Popen. class Popen(...): ... def signal(self, signal): """Send a signal to the process (UNIX only) signal is constant from the signal module """ def terminate(self, force=False): """Terminate the process On UNIX terminate(False) is equivalent to signal(SIGTERM) and terminate(True) to signal(SIGKILL). On Windows ... (does Windows support a forced terminate?) """
Here is what my process.py [1] does for termination (not suggesting you follow the API here, just showing how it handles termination on Windows). Some of the Windows logic is borrowed from PyWin32's winprocess.py [2]
def kill(self, exitCode=0, gracePeriod=1.0, sig=None): """Kill process.
"exitCode" [deprecated, not supported] (Windows only) is the code the terminated process should exit with. "gracePeriod" (Windows only) is a number of seconds the process is allowed to shutdown with a WMCLOSE signal before a hard terminate is called. "sig" (Unix only) is the signal to use to kill the process. Defaults to signal.SIGKILL. See os.kill() for more information. Windows: Try for an orderly shutdown via WMCLOSE. If still running after gracePeriod (1 sec. default), terminate. """ if sys.platform.startswith("win"): import win32gui # Send WMCLOSE to windows in this process group. win32gui.EnumWindows(self.close, 0) # Send Ctrl-Break signal to all processes attached to this # console. This is supposed to trigger shutdown handlers in # each of the processes. try: win32api.GenerateConsoleCtrlEvent(CTRLBREAKEVENT, self.processId) except AttributeError: _log.warn("The win32api module does not have "_ _"GenerateConsoleCtrlEvent(). This may mean that "_ "parts of this process group have NOT been killed.") except win32api.error, ex: if ex.args[0] not in (6, 87): # Ignore the following: # apierror: (87, 'GenerateConsoleCtrlEvent', 'The parameter is incorrect.') # apierror: (6, 'GenerateConsoleCtrlEvent', 'The handle is invalid.') # Get error 6 if there is no console. raise # Last resort: call TerminateProcess if it has not yet. retval = 0 try: self.wait(gracePeriod) except ProcessError, ex: log.info("[%s] Process.kill: calling TerminateProcess", id(self)) win32process.TerminateProcess(self.hProcess, -1) win32api.Sleep(100) # wait for resources to be released else: if sig is None: sig = signal.SIGKILL try: os.kill(self.pid, sig) except OSError, ex: if ex.errno != 3: # Ignore: OSError: [Errno 3] No such process raise
Links: [1] http://trentm.com/projects/process/ [2] http://pywin32.cvs.sourceforge.net/pywin32/pywin32/win32/Demos/winprocess.py?revision=1.2&view=markup#l_121
-- Trent Mick trentm at activestate.com
- Previous message: [Python-Dev] About SSL tests
- Next message: [Python-Dev] About SSL tests
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]