Skip to content
tezvyn:

asyncio Streams: High-Level Async Network I/O

Source: python.readthedocs.ioMediumHow cards are made

asyncio Streams: High-Level Async Network I/O

asyncio Streams are like async file handles for the network. You get a reader/writer pair to await data, simplifying TCP clients and servers for basic protocols. The footgun: the default buffer limit is small; reading large data will fail unexpectedly.

Why it exists

The lower-level asyncio APIs (Transports and Protocols) are powerful but complex, requiring you to manage connection state and event callbacks. The Streams API was created to provide a simpler, higher-level abstraction for the common case of stream-oriented network communication, making it feel more like familiar file I/O.

The mental model

Think of an asyncio Stream as an asynchronous, two-way pipe for bytes over a network. When you establish a connection, you get two objects: a StreamReader to read incoming data from and a StreamWriter to send outgoing data to. You await reading from the reader and you write() then await drain() on the writer.

How it works

The primary entry points are asyncio.open_connection() for clients and asyncio.start_server() for servers. These coroutines handle the underlying socket connection and protocol setup. They return a (reader, writer) tuple. The StreamReader maintains an internal buffer. When you call a method like reader.read(1024), it first checks its buffer. If not enough data is present, it asynchronously waits for more data to arrive from the network before returning. The StreamWriter buffers your writes and sends them efficiently over the transport.

When to use it

Use the Streams API for building custom, stream-based network clients and servers in Python. It's ideal for simple, bespoke protocols, like a basic chat server, a remote command executor, or a simple data replication service. It is the standard library's most straightforward tool for async socket programming.

When not to use it

Avoid it for implementing standard, complex protocols like HTTP or WebSocket. For those, use a dedicated, feature-rich library like aiohttp or httpx. If your protocol is highly stateful and event-driven (not just a simple request-response), you might get more control by dropping down to the lower-level asyncio.Protocol API.

One canonical example

A simple echo server. A client connects using open_connection, gets a reader and writer, and sends a message with writer.write(b'hello'). The server, created with start_server, has a handler function that receives its own reader and writer for that client. The handler calls await reader.read(100) to get the message, then sends it back with writer.write(data) and await writer.drain() to ensure it's sent. Finally, both sides close the connection.

Interview question

For which scenario would asyncio Streams be the most appropriate choice for network communication?

  • a.Building a high-performance HTTP/2 server from scratch.
  • b.Developing a highly stateful, event-driven game server.
  • c.Processing extremely large, continuous data streams without concern for memory.
  • d.Implementing a custom binary protocol for a simple chat application.Correct
Why?

The card states that Streams are ideal for "simple, bespoke protocols, like a basic chat server," making option D correct. Options A and B are explicitly listed as scenarios where Streams are not recommended, suggesting dedicated libraries or the lower-level Protocol API. Option C describes a limitation of Streams, which have a small default buffer limit.

Just read this? Test yourself on what you have been reading.

Read the original → python.readthedocs.io

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on python — each one lists the topics its interview covers.

See open roles