Python Glossary Русский

Python 3.4+Async programmingterm

asyncio.BaseTransport

protocols and transports

The base class of transports — objects asyncio hands to a protocol to work with a socket, pipe or process: close(), is_closing(), get_extra_info().

Syntax

transport.get_extra_info(name, default=None); transport.close(); transport.is_closing()

Learners rarely need this layer: streams (asyncio.open_connection() and asyncio.start_server()) cover TCP, and asyncio.create_subprocess_exec() covers processes. You never create a transport yourself: loop.create_connection() and its siblings return one, and the protocol receives it in connection_made(). close() is not immediate — the write buffer is flushed asynchronously and connection_lost() comes later. get_extra_info('peername') or 'socket' is how you learn the peer address or reach the low-level object; StreamWriter has the same method.

Examples

import asyncio
import socket
async def main():
    loop = asyncio.get_running_loop()
    left, right = socket.socketpair()
    transport, proto = await loop.create_connection(asyncio.Protocol, sock=left)
    print(isinstance(transport, asyncio.BaseTransport))  # → True
    print(transport.get_extra_info('socket') is not None)  # → True
    transport.close()
    print(transport.is_closing())  # → True
    await asyncio.sleep(0.01)
    right.close()
asyncio.run(main())

See also

Open in the full glossarydocs.python.orgReport an inaccuracy