More on frameworks
I've broken from our usual procedural form and used an object-oriented approach in the last example for two reasons. First, although it is possible to write a purely procedural style server with asyncio
, it requires a deeper understanding of co-routines than what we were able to provide here. If you're curious, then you can go through an example co-routine style echo server, which is in the asyncio
documentation at https://docs.python.org/3/library/asyncio-stream.html#asyncio-tcp-echo-server-streams.
The second reason is that this kind of class-based approach is generally a more manageable model to follow in a full system.
There is in fact a new module called selectors
in Python 3.4, which provides an API for quickly building an object-oriented server based on the IO primitives in the select
module (including poll
). The documentation and an example can be seen at https://docs.python.org/3.4/library/selectors.html.
There are other third-party event-driven frameworks available...