Python Glossary Русский

Python 3.4+Async programmingterm

asyncio.SubprocessTransport

protocols and transports

The transport of a child process: get_pid(), get_returncode(), get_pipe_transport(fd), terminate() and kill(). loop.subprocess_exec() returns it.

Syntax

transport, protocol = await loop.subprocess_exec(Proto, program, *args)

Learners rarely need this layer: streams (asyncio.open_connection() and asyncio.start_server()) cover TCP, and asyncio.create_subprocess_exec() covers processes. The Process object from create_subprocess_exec() wraps exactly this transport and offers pid, returncode, wait() and communicate(). get_returncode() returns None while the process is alive — that is not 'exit code 0'. Close the transport with close(), or garbage collection emits a ResourceWarning.

Examples

import asyncio
import sys
class Waiter(asyncio.SubprocessProtocol):
    def __init__(self, done):
        self.done = done
    def process_exited(self):
        self.done.set_result(True)
async def main():
    loop = asyncio.get_running_loop()
    done = loop.create_future()
    transport, _ = await loop.subprocess_exec(
        lambda: Waiter(done), sys.executable, '-c', 'raise SystemExit(3)',
        stdin=None, stdout=None, stderr=None)
    print(isinstance(transport.get_pid(), int))  # → True
    await done
    print(transport.get_returncode())  # → 3
    transport.close()
asyncio.run(main())

See also

Open in the full glossarydocs.python.orgReport an inaccuracy