Users Online

· Guests Online: 144

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

Articles Hierarchy

Python Stream

Python Stream

Python Stream

Introduction to Python stream

In this article, streams in Python are defined as async or await primitives and these are high-level primitives, streams are mainly used to work with network connections. In Python, streams are also used in IO modules to provide various types of input/output. But in this article, we are discussing Python stream which is used for sending and receiving information and these do not use any callbacks or protocols and transport while sending and receiving any data. There are different stream functions and uses asyncio modules for sending and receiving data. In this article, we are discussing stream which is only supported by the Python newer versions that is Python 3.4 only.

Functions of Python Stream

In this article, we will see how Python stream is used for sending and receiving any pieces of information without any protocols or callbacks. 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, to create streams, etc.

Now let us see stream functions in detail:

1. Open_connection()

This function is used as coroutine asyncio.open_connection() which is mainly used as a wrapper for creating connections along with create_connection() where this creates function returns a pair which contains reader and writer pair which are instances of StreamReader and StreamWriter respectively. 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 create_connection() function

2. Start_server()

This function is also stream function for starting the server this also uses coroutine along with asyncio module and is declared as coroutine asyncio.start_server(). These functions also have various parameters and this function is used to start socket server. The function start_server( client_connected_cb, host, port, loop, limit,..) where these parameters use client_connected_cb which is coroutine function which is used for automatically scheduling tasks, loop parameter is used for determining when a method is awaited from a coroutine, the limit parameter is used for specifying buffer size limit which is by default 64KiB, and the all other different parameters are directly passed to other function like loop.create_server().

These above two functions have Unix based functions also which are known for Unix sockets which can be used to operate on Unix sockets. The above two functions are altered as open_unix_connection() and start_unix_connection() functions along with similar parameters and the only difference is instead of using host and port parameters in this Unix socket-based function we use path parameter for using path-like objects. These functions work only with Unix Sockets.

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.

StreamReader in Python stream is declared as “class asyncio.StreamReader( limit, loop)” where limit parameter takes default value as 64KiB. We have to note that we cannot directly initiate objects instead we have to use with open_connection() and start_server() functions. This StreamReader provides different functions such as exception() which is used to get the exceptions, feed_eof() this is used to acknowledge about the end of the file, feed_data() to send the data in bytes in the internal buffer, set_exception() which is used for setting up the obtained exception, etc.

This also supports few coroutine functions such as coroutine read(n = -1 ) which is for reading n bytes and if not specified it takes by default value as -1 that means until the end of the file and this will return all the bytes that are read, coroutine readline() this is used for reading the single line as it will not encounter newline character (\n), coroutine readexactly(n) which is used for reading exact bytes and this can raise an exception IncompleteReadError if the end of file is reached without reading the bytes specified in the argument and it can also raise another exception to read partial bytes using the attribute as IncompleteReadError.partial, coroutine readuntil(delimiter) this is used to read the bytes until it encounters the specified delimiter or separator and returns the data in bytes and the delimiter is added at the end of the data this coroutine function also raises various exceptions like above.

StreamReaderProtocol is also class provided by stream python to adapt between protocol and StreamReader and this is a subclass of the protocol. This StreamReader makes itself a subclass of the protocol because it has the potential to prevent it from calling inappropriate methods of the protocol.

StreamWriter in Python stream is declared as “class asyncio.StreamWriter( transport, protocol, reader, loop) where this also provides various functions like writing () is used to write the data to transport, whitelines() used to write a list of lines to transport without encountering newline, get_extra_info() this is used to return a piece of extra optional information about transport, write_eof() this is used to close the write of the transport at the end of the file after flushing all the buffer data from the transport, can_write_eof() is used when it supports write_eof it returns true else false, the close() function is for closing the transport, etc. These StreamWrier functions work similarly to that in StreamReader. There is also coroutine function such as coroutine drain() which is used for flushing buffer data of underlying transport.

Conclusion

In this article, we conclude that Python stream is defined for sending and receiving data such as sending and receiving information between client and server using various Stream functions such as open_connection(), start_server(), etc. In this article, we also saw stream classes such as StreamReader and StreamWriter which provides instances like reader and writer that are used in stream functions. These stream classes provide different functions for reading and writing the information to the transport or buffer.

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 1.32 seconds
10,814,105 unique visits