liveBook · Manning

Stream writer

As explained in Python Concurrency with asyncio

A stream writer is an object that provides operations for sending data through a network stream.

A stream writer is an asyncio object for sending data through a network stream. It hides socket details behind a small interface and is usually paired with a StreamReader for receiving data. section 8.0 section 8.3

Use a stream writer to:

  • Send bytes with write(); the method is immediate and is not a coroutine. section 8.3
  • Represent a connected client in server callbacks, so application code can send responses or notifications. section 8.5
  • Store connected writers to broadcast messages to multiple clients. section 8.5 section 8.6

Sending an HTTP request

Obtain a reader and writer with await asyncio.open_connection(host, port), encode the request, call stream_writer.write(request), and await stream_writer.drain(). Read the response through the paired StreamReader. section 8.3

Listing 8.3 A HTTP request with stream readers and writers
import asyncio
from asyncio import StreamReader
from typing import AsyncGenerator
 
 
async def read_until_empty(stream_reader: StreamReader) -> AsyncGenerator[str, None]:
    while response := await stream_reader.readline():                                                  #1
        yield response.decode()
 
 
async def main():
    host: str = 'www .example .com'
    request: str = f"GET / HTTP/1.1\r\n" \
                   f"Connection: close\r\n" \
                   f"Host: {host}\r\n\r\n"
 
    stream_reader, stream_writer = await asyncio.open_connection('www .example .com', 80)
 
    try:
        stream_writer.write(request.encode())                                                          #2
        await stream_writer.drain()
 
        responses = [response async for response in read_until_empty(stream_reader)]                   #3
 
        print(''.join(responses))
    finally:
        stream_writer.close()                                                                          #4
        await stream_writer.wait_closed()
 
 
asyncio.run(main())

Inspect the complete HTTP example to see connection creation, request encoding, write(), drain(), and asynchronous response reading together. section 8.3

How Python Concurrency with asyncio covers it

How deep it goes
Explains write buffering and drain usage, then applies writers in HTTP, echo-server, and chat examples without detailing socket internals.
Examples
Uses an HTTP client, an echo server, a chat server, and a chat client to demonstrate sending and broadcasting data.
What it assumes
Assumes basic asyncio and networking familiarity while introducing stream-based communication as an easier alternative to sockets.
What you can do after
Build network clients and servers that send buffered data through asyncio streams.
How it connects
Connects StreamWriter with StreamReader, transports and protocols, sockets, open_connection, and start_server.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Concurrency with asyncio. The section links point to the parts of the book it draws on.