Python Glossary Русский

Python 3.4+Async programmingterm

asyncio.ReadTransport

protocols and transports

The interface of a transport you read from: pause_reading(), resume_reading() and is_reading() control the flow of data into data_received().

Syntax

transport.pause_reading(); transport.resume_reading(); transport.is_reading()

Learners rarely need this layer: streams (asyncio.open_connection() and asyncio.start_server()) cover TCP, and asyncio.create_subprocess_exec() covers processes. pause_reading() is manual backpressure: when the protocol cannot keep up with incoming data, you pause reading from the socket, otherwise it piles up in memory. With streams, StreamReader does this itself when its buffer fills up (the limit parameter). Calling pause_reading() or resume_reading() twice is safe — since 3.7 the second call is a no-op.

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.ReadTransport))  # → True
    transport.pause_reading()
    print(transport.is_reading())  # → False
    transport.resume_reading()
    print(transport.is_reading())  # → True
    transport.close()
    await asyncio.sleep(0.01)
    right.close()
asyncio.run(main())

See also

Open in the full glossarydocs.python.orgReport an inaccuracy