How to create a server protocol class with __init__ function?

I come from the background of Twisted, so I have a clear understanding of the protocols and factories implemented by Twisted. However, I am moving on to switching to asyncio, and I am having trouble understanding how factories integrate into this particular structure.

The official documentation provides an example of a server class definition asyncio.Protocol. It does not have a user-defined function __init__, so we can just call it loop.create_server(EchoServerClientProtocol, addr, port).

What happens if ours Protocolneeds to implement some initialization logic? For example, consider this example, which sets the maximum buffer size:

import asyncio
from collections import deque

class BufferedProtocolExample(asyncio.Protocol):
    def __init__(self, buffsize=None):
        self.queue = deque((), buffsize)

    # ...

In Twisted, you create a class Factoryto hold all configuration values, which are then passed to the function that initializes the connection. Asyncio seems to work similarly , but I cannot find any documentation.

I could use functools.partial, but what is the correct way to handle this case?

+4
source share
1 answer

, , , functools.partial . , protocol_factory . - Twisted Factorys, __call__ , buildProtocol Twisted.

+5

Source: https://habr.com/ru/post/1607149/


All Articles