Transports and protocols in asyncio

The asyncio module provides a number of different transport classes. In essence, these classes are the implementations of the functionalities of the transport layer that were discussed in the preceding section. You already know that the transport layer plays an integral role in communication channels; the transport classes, therefore, give asyncio (and consequently, the developers) more control over the process of implementing our own communication channels.

The asyncio module combines the abstract of transports with the implementation of an asynchronous program. Specifically, even though transports are the central elements of communication channels, in order to utilize the transport classes and other relevant communication channel tools, we need to initiate and call an event loop, which is an instance of the asyncio.AbstractEventLoop class. The event loop itself will then create the transports and manage the low-level communication procedures.

It is important to note that a transport object in an established communication channel in asyncio is always associated with an instance of the asyncio.Protocol class. As the name suggests, the Protocol class specifies the underlying protocols that the communication channels use; for each connection made with another system, a new protocol object from this class will be created. While working closely with a transport object, a protocol object can call various methods from the transport object; this is the point where we can implement the specific inner workings of a communication channel.

For this reason, generally we need to focus on the implementation of an asyncio.Protocol subclass and its methods while building a connection channel. In other words, we use asyncio.Protocol as a parent class to derive a subclass that meets the needs of our communication channel. To do this, we overwrite the following methods from the asyncio.Protocol base class in our own custom protocol subclass:

  • Protocol.connection_made(transport): This method is automatically called whenever a connection from another system is made. The transport argument holds the transport object that is associated with the connection. Again, each transport needs to be paired with a protocol; we generally store this transport object as an attribute of this specific protocol object in the connection_made() method.
  • Protocol.data_received(data): This method is automatically called whenever the one system that we are connected to sends its data. Note that the data argument, which holds the sent information, is usually represented in bytes, so the encode() function of Python should be used before data is processed further.

Next, let us consider the important methods from the transport classes from asyncio. All transport classes inherit from a parent transport class, called asyncio.BaseTransport, for which we have the following common methods:

  • BaseTransport.get_extra_info(): This method returns, as the name suggests, additional channel-specific information for the calling transport object. The result can include information regarding the socket, the pipe, and the subprocess associated with that transport. Later in this chapter, we will be calling BaseTransport.get_extra_info('peername'), in order to obtain the remote address from which the transport traveled.
  • BaseTransport.close(): This method is used to close the calling transport object, after which the connections between different systems will be stopped. The corresponding protocol of the transport will automatically call its connection_lost() method.

Out of the many implementations of transport classes, we will focus on the asyncio.WriteTransport class, which again inherits the methods from the BaseTransport class, and additionally implements other methods that are used to facilitate write-only transport functionalities. Here, we will be using the WriteTransport.write() method, which will write the data that we would like to send to the other system that we communicate with via the transport object. As a part of the asyncio module, this method is not a blocking function; instead, it buffers and sends out the written data in an asynchronous way.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.144.228