Python Glossary Русский

Python 3.4+Async programmingterm

asyncio.Transport

protocols and transports

A bidirectional asyncio transport: a connection you can both write to and read from. It combines ReadTransport and WriteTransport.

Syntax

transport.write(data); transport.close(); transport.abort(); transport.get_extra_info(name)

You never construct a transport yourself: the loop hands one out (loop.create_connection, create_server), or you reach it from a stream as writer.transport. The transport is "how bytes travel" and the protocol is "what to do with them"; incoming data arrives through protocol.data_received(), not as a return value of read(). close() flushes the buffer first while abort() drops it and cuts the connection at once; is_closing() turns True right after close(), although the socket closes on a later loop iteration.

Examples

import asyncio
import socket
async def main():
    a, b = socket.socketpair()
    reader, writer = await asyncio.open_connection(sock=a)
    t = writer.transport
    print(isinstance(t, asyncio.Transport))  # → True
    print(isinstance(t, asyncio.ReadTransport), isinstance(t, asyncio.WriteTransport))  # → True True
    print(t.is_closing())  # → False
    t.close()
    print(t.is_closing())  # → True — закрытие уже начато
    await asyncio.sleep(0.01)
    b.close()
asyncio.run(main())

See also

Open in the full glossarydocs.python.orgReport an inaccuracy