Updated June 30, 2023
Introduction to Python stream
In this article, we explore streams in Python as async or await primitives, which are high-level constructs that facilitate working with network connections. In Python, streams are utilized in IO modules to facilitate different input/output operations. However, this article specifically focuses on Python streams that are used for sending and receiving information without relying on callbacks, protocols, or transport mechanisms. The article covers various stream functions and demonstrates their usage with the asyncio module for sending and receiving data.
Functions of Python Stream
In this section, we will discuss the stream functions. The stream functions are functions that are top-level asyncio functions to work with the stream, create streams, etc.
Now let us see stream functions in detail:
The function asyncio.open_connection() is used as a coroutine and serves as a wrapper for establishing connections. It works in conjunction with the create_connection() function, which returns a pair containing instances of StreamReader and StreamWriter. This stream function takes parameters such as coroutine asyncio.open_connection( host, port, *, loop, limit, **kwds). These arguments are usually used for creating a connection with the create_connection() function
The function “asyncio.start_server()” is a stream function used to start a socket server in Python. It uses coroutine along with the asyncio module. The function is declared as “asyncio.start_server(client_connected_cb, host, port, loop, limit, …)”. The parameters of the “start_server()” function are as follows:
The parameter “client_connected_cb” specifies a coroutine function that automatically schedules tasks when a new client connects to the server. It handles the client’s requests and interactions.
The “host” parameter specifies the IP address or hostname on which the server will listen for incoming connections. It can listen on all available interfaces by setting it to an empty string or “None.”
What is Python Stream?
In this Python stream, we have seen we have instances like reader and writer of StreamReader and StreamWriter. We will see in detail these in the below section. This StreamReader makes itself a subclass of the protocol because it can potentially prevent it from calling inappropriate methods of the protocol.
The StreamWriter class in the Python asyncio stream is declared as “class asyncio.StreamWriter(transport, protocol, reader, loop)”. It provides various functions for interacting with the transport:
The write() function is used to write data to the transport.
The writelines() function is used to write a list of lines to the transport without encountering newline characters.
The get_extra_info() function returns optional extra information about the transport.
The write_eof() function closes the write end of the transport after flushing all the buffered data.
The can_write_eof() function checks if the transport supports write_eof and returns true or false accordingly.
The close() function is used to close the transport.
In this article, we also saw stream classes such as StreamReader and StreamWriter, which provide instances like reader and writer used in stream functions. These stream classes provide different functions for reading and writing the information to the transport or buffer.
This is a guide to Python Stream. Here we discuss an introduction to Python Stream and the stream functions with detailed explanation. You can also go through our other related articles to learn more –