mirror of
https://github.com/python/cpython.git
synced 2025-07-28 13:44:43 +00:00
Added kill, terminate and send_signal to subprocess.Popen
The bits and pieces for the Windows side were already in place. The POSIX side is trivial (as usual) and uses os.kill().
This commit is contained in:
parent
c873550737
commit
e74c8f2879
4 changed files with 103 additions and 0 deletions
|
@ -357,6 +357,7 @@ import os
|
|||
import types
|
||||
import traceback
|
||||
import gc
|
||||
import signal
|
||||
|
||||
# Exception classes used by this module.
|
||||
class CalledProcessError(Exception):
|
||||
|
@ -384,6 +385,7 @@ if mswindows:
|
|||
from win32process import CreateProcess, STARTUPINFO, \
|
||||
GetExitCodeProcess, STARTF_USESTDHANDLES, \
|
||||
STARTF_USESHOWWINDOW, CREATE_NEW_CONSOLE
|
||||
from win32process import TerminateProcess
|
||||
from win32event import WaitForSingleObject, INFINITE, WAIT_OBJECT_0
|
||||
else:
|
||||
from _subprocess import *
|
||||
|
@ -906,6 +908,21 @@ class Popen(object):
|
|||
self.wait()
|
||||
return (stdout, stderr)
|
||||
|
||||
def send_signal(self, sig):
|
||||
"""Send a signal to the process
|
||||
"""
|
||||
if sig == signal.SIGTERM:
|
||||
self.terminate()
|
||||
else:
|
||||
raise ValueError("Only SIGTERM is supported on Windows")
|
||||
|
||||
def terminate(self):
|
||||
"""Terminates the process
|
||||
"""
|
||||
TerminateProcess(self._handle, 1)
|
||||
|
||||
kill = terminate
|
||||
|
||||
else:
|
||||
#
|
||||
# POSIX methods
|
||||
|
@ -1184,6 +1201,21 @@ class Popen(object):
|
|||
self.wait()
|
||||
return (stdout, stderr)
|
||||
|
||||
def send_signal(self, sig):
|
||||
"""Send a signal to the process
|
||||
"""
|
||||
os.kill(self.pid, sig)
|
||||
|
||||
def terminate(self):
|
||||
"""Terminate the process with SIGTERM
|
||||
"""
|
||||
self.send_signal(signal.SIGTERM)
|
||||
|
||||
def kill(self):
|
||||
"""Kill the process with SIGKILL
|
||||
"""
|
||||
self.send_signal(signal.SIGKILL)
|
||||
|
||||
|
||||
def _demo_posix():
|
||||
#
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue