Python Glossary Русский

Python 3.4+Async programmingterm

asyncio.WriteTransport

protocols and transports

A write-only transport: write() puts bytes into a buffer and returns immediately, while the buffer watermarks drive back-pressure.

Syntax

transport.write(data); transport.writelines(list_of_data); transport.set_write_buffer_limits(high=None, low=None)

write() is not a coroutine and never waits: with a slow peer the buffer grows without bound until the protocol honours pause_writing()/resume_writing(). set_write_buffer_limits(high, low) sets the thresholds, 64 KiB and 16 KiB by default. With streams the same job is done by await writer.drain(), which is why a write loop must not skip it. write_eof() half-closes the write side when can_write_eof() is true.

Examples

import asyncio
import socket
async def main():
    a, b = socket.socketpair()
    reader, writer = await asyncio.open_connection(sock=a)
    t = writer.transport
    print(t.get_write_buffer_limits())  # → (16384, 65536) — (low, high)
    t.write(b'ping')  # без await: байты ушли в буфер
    print(t.can_write_eof())  # → True
    await asyncio.sleep(0.01)
    print(b.recv(10))  # → b'ping'
    t.close()
    await asyncio.sleep(0.01)
    b.close()
asyncio.run(main())

See also

Open in the full glossarydocs.python.orgReport an inaccuracy