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 WM_CLOSE 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 WM_CLOSE. If still running > after gracePeriod (1 sec. default), terminate. > """ > if sys.platform.startswith("win"): > import win32gui > # Send WM_CLOSE 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(CTRL_BREAK_EVENT, > 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: > # api_error: (87, 'GenerateConsoleCtrlEvent', 'The > parameter is incorrect.') > # api_error: (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 _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com