An asyncio-based chat server
The asyncio Standard Library module is new in Python 3.4 and it is an effort at bringing some standardization around asynchronous I/O into the Standard Library. The asyncio library uses a co-routine based style of programming. It provides a powerful loop class, which our programs can submit prepared tasks, called co-routines, to, for asynchronous execution. The event loop handles the scheduling of the tasks and optimization of performance around blocking I/O calls.
It has built-in support for socket-based networking, which makes building a basic server a straightforward task. Let's see how this can be done. Create a new file called 5.1-chat_server-asyncio.py and save the following code in it:
import asyncio
import tincanchat
HOST = tincanchat.HOST
PORT = tincanchat.PORT
clients = []
class ChatServerProtocol(asyncio.Protocol):
""" Each instance of class represents a client and the socket
connection to it. """
def connection_made(self, transport...