What are the requirements of a protocol factory in python asyncio?
<p>I am looking at the example of the <a href="https://docs.python.org/3/library/asyncio-protocol.html#udp-echo-server-protocol" rel="nofollow">UDP echo server</a>:</p>

<pre><code>import asyncio

class EchoServerProtocol:
    def connection_made(self, transport):
        self.transport = transport

    def datagram_received(self, data, addr):
        message = data.decode()
        print('Received %r from %s' % (message, addr))
        print('Send %r to %s' % (message, addr))
        self.transport.sendto(data, addr)

loop = asyncio.get_event_loop()
print("Starting UDP server")
# One protocol instance will be created to serve all client requests
listen = loop.create_datagram_endpoint(
    EchoServerProtocol, local_addr=('127.0.0.1', 9999))
transport, protocol = loop.run_until_complete(listen)

try:
    loop.run_forever()
except KeyboardInterrupt:
    pass

transport.close()
loop.close()
</code></pre>

<p>It seems that the call...</p>

<p><code>loop.create_datagram_endpoint(EchoServerProtocol, local_addr=('127.0.0.1', 9999))</code></p>

<p>...is doing all the work here. The <a href="https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.AbstractEventLoop.create_datagram_endpoint" rel="nofollow">method documentation</a> states the following for the first argument (<code>protocol_factory</code>):</p>

<blockquote>
  <p>protocol_factory must be a callable returning a protocol instance.</p>
</blockquote>

<p>My questions:</p>

<ul>
<li>What defines a <code>protocol instance</code>?</li>
<li>Is <code>returning a protocol instance</code> a different wording for <code>initiating a protocol object</code>?</li>
<li>How does the <code>EchoServerProtocol</code> in the example fulfill this requirement?</li>
</ul>

<h3>What defines a <em>protocol instance</em>?</h3>

<p>A protocol is a class you define that implements one of the interfaces defined in the <a href="https://docs.python.org/3/library/asyncio-protocol.html#protocol-classes" rel="nofollow">Protocols section</a>, i.e. provides implementations for a set of callbacks, e.g. <a href="https://docs.python.org/3/library/asyncio-protocol.html#connection-callbacks" rel="nofollow">Connection Callbacks</a>.</p>

<p>So for the UDP echo server example you have posted, the <code>EchoServerProtocol</code> user defined class actually defines a protocol by implementing the <a href="https://docs.python.org/3/library/asyncio-protocol.html#asyncio.BaseProtocol.connection_made" rel="nofollow"><code>connection_made</code></a> and <a href="https://docs.python.org/3/library/asyncio-protocol.html#asyncio.DatagramProtocol.datagram_received" rel="nofollow"><code>datagram_received</code></a>.</p>

<p>In summary, if you implement one of those callbacks in a class, you are said to be defining a <code>Protocol</code>. So an instance/object of that class would be a <strong>protocol instance</strong>.</p>

<hr>

<h3>Is <em>returning a protocol instance</em> a different wording for <em>initiating a protocol object</em>?</h3>

<p>Formally <em>YES</em>. Before you would return a <strong>protocol instance</strong>, you would have <strong>initialized</strong> it. So basically one is a prerequisite of the other.</p>

<hr>

<h3>How does the <code>EchoServerProtocol</code> in the example fulfill this requirement?</h3>

<p>So first of all, as answered the first question, the <code>EchoServerProtocol</code> defines a protocol. Thus the next thing is to provide a <code>protocol_factory</code>, which is <a href="https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.AbstractEventLoop.create_datagram_endpoint" rel="nofollow">defined</a> as:</p>

<blockquote>
  <p><code>protocol_factory</code> must be a callable returning a protocol instance.</p>
</blockquote>

<p>So to satisfy this requirement, you could just have this simple method:</p>

<pre><code>def my_protocol_factory():
    return EchoServerProtocol()
</code></pre>

<p>Note, that this factory first initializes the protocol instance and then returns it.</p>

<p>So the thing that might confuse you in the example, is that the class <code>EchoServerProtocol</code> itself is passed as the <code>protocol_factory</code>, but if you summarize what I've said, you will see that the <code>EchoServerProtocol</code> is actually a callable, and when it gets called, i.e. <code>EchoServerProtocol()</code> it actually initializes a <code>EchoServerProtocol</code> instance, and returns it.</p>

<p>So yep, the example fulfills the requirement.</p>

<hr>

python python-3.5 python-asyncio