mirror of
https://github.com/python/cpython.git
synced 2025-08-25 11:15:02 +00:00
asyncio: sync with Tulip
Issue #23347: send_signal(), kill() and terminate() methods of BaseSubprocessTransport now check if the transport was closed and if the process exited. Issue #23347: Refactor creation of subprocess transports. Changes on BaseSubprocessTransport: * Add a wait() method to wait until the child process exit * The constructor now accepts an optional waiter parameter. The _post_init() coroutine must not be called explicitly anymore. It makes subprocess transports closer to other transports, and it gives more freedom if we want later to change completly how subprocess transports are created. * close() now kills the process instead of kindly terminate it: the child process may ignore SIGTERM and continue to run. Call explicitly terminate() and wait() if you want to kindly terminate the child process. * close() now logs a warning in debug mode if the process is still running and needs to be killed * _make_subprocess_transport() is now fully asynchronous again: if the creation of the transport failed, wait asynchronously for the process eixt. Before the wait was synchronous. This change requires close() to *kill*, and not terminate, the child process. * Remove the _kill_wait() method, replaced with a more agressive close() method. It fixes _make_subprocess_transport() on error. BaseSubprocessTransport.close() calls the close() method of pipe transports, whereas _kill_wait() closed directly pipes of the subprocess.Popen object without unregistering file descriptors from the selector (which caused severe bugs). These changes simplifies the code of subprocess.py.
This commit is contained in:
parent
978a9afc6a
commit
47cd10d7a9
6 changed files with 167 additions and 105 deletions
|
@ -25,8 +25,6 @@ class SubprocessStreamProtocol(streams.FlowControlMixin,
|
|||
super().__init__(loop=loop)
|
||||
self._limit = limit
|
||||
self.stdin = self.stdout = self.stderr = None
|
||||
self.waiter = futures.Future(loop=loop)
|
||||
self._waiters = collections.deque()
|
||||
self._transport = None
|
||||
|
||||
def __repr__(self):
|
||||
|
@ -61,9 +59,6 @@ class SubprocessStreamProtocol(streams.FlowControlMixin,
|
|||
reader=None,
|
||||
loop=self._loop)
|
||||
|
||||
if not self.waiter.cancelled():
|
||||
self.waiter.set_result(None)
|
||||
|
||||
def pipe_data_received(self, fd, data):
|
||||
if fd == 1:
|
||||
reader = self.stdout
|
||||
|
@ -94,16 +89,9 @@ class SubprocessStreamProtocol(streams.FlowControlMixin,
|
|||
reader.set_exception(exc)
|
||||
|
||||
def process_exited(self):
|
||||
returncode = self._transport.get_returncode()
|
||||
self._transport.close()
|
||||
self._transport = None
|
||||
|
||||
# wake up futures waiting for wait()
|
||||
while self._waiters:
|
||||
waiter = self._waiters.popleft()
|
||||
if not waiter.cancelled():
|
||||
waiter.set_result(returncode)
|
||||
|
||||
|
||||
class Process:
|
||||
def __init__(self, transport, protocol, loop):
|
||||
|
@ -124,30 +112,18 @@ class Process:
|
|||
|
||||
@coroutine
|
||||
def wait(self):
|
||||
"""Wait until the process exit and return the process return code."""
|
||||
returncode = self._transport.get_returncode()
|
||||
if returncode is not None:
|
||||
return returncode
|
||||
"""Wait until the process exit and return the process return code.
|
||||
|
||||
waiter = futures.Future(loop=self._loop)
|
||||
self._protocol._waiters.append(waiter)
|
||||
yield from waiter
|
||||
return waiter.result()
|
||||
|
||||
def _check_alive(self):
|
||||
if self._transport.get_returncode() is not None:
|
||||
raise ProcessLookupError()
|
||||
This method is a coroutine."""
|
||||
return (yield from self._transport.wait())
|
||||
|
||||
def send_signal(self, signal):
|
||||
self._check_alive()
|
||||
self._transport.send_signal(signal)
|
||||
|
||||
def terminate(self):
|
||||
self._check_alive()
|
||||
self._transport.terminate()
|
||||
|
||||
def kill(self):
|
||||
self._check_alive()
|
||||
self._transport.kill()
|
||||
|
||||
@coroutine
|
||||
|
@ -221,11 +197,6 @@ def create_subprocess_shell(cmd, stdin=None, stdout=None, stderr=None,
|
|||
protocol_factory,
|
||||
cmd, stdin=stdin, stdout=stdout,
|
||||
stderr=stderr, **kwds)
|
||||
try:
|
||||
yield from protocol.waiter
|
||||
except:
|
||||
transport._kill_wait()
|
||||
raise
|
||||
return Process(transport, protocol, loop)
|
||||
|
||||
@coroutine
|
||||
|
@ -241,9 +212,4 @@ def create_subprocess_exec(program, *args, stdin=None, stdout=None,
|
|||
program, *args,
|
||||
stdin=stdin, stdout=stdout,
|
||||
stderr=stderr, **kwds)
|
||||
try:
|
||||
yield from protocol.waiter
|
||||
except:
|
||||
transport._kill_wait()
|
||||
raise
|
||||
return Process(transport, protocol, loop)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue