[Tutor] Using asyncio in event-driven network library

Mark Lawrence breamoreboy at yahoo.co.uk
Sun Dec 22 16:50:33 CET 2013


On 22/12/2013 14:43, Tobias M. wrote:
> Hello,
>
> I am currently writing an event-driven client library for a network
> protocol [1] and chose to use the new asyncio module. I have no
> experience with asynchronous IO and don't understand all the concepts in
> asyncio yet. So I'm not sure if asyncio is actually the right choice .
>
> My goal:
> I want to provide an easy to use interface to the network protocol.
>
> What I have so far:
> I internally use the asyncio event loop and and the user can register
> event handler functions for specific events triggered by the network input.
> Reduced to the essentials the architecture of my code looks like this:
>
> class Client(asyncio.Protocol):
>
>      def __init__(self, handler, server, port):
>          self._handler = handler
>          self._server = server
>          self._port = port
>
>      def run(self):
>          loop = asyncio.get_event_loop()
>          task = asyncio.Task(loop.create_connection(self, server, port))
>          loop.run_until_complete(task)
>          loop.run_forever()
>
>      def data_received(self, data):
>          # read data and call the appropriate handler methods on
> self._handler
>          (..)
>
> The user initializes a Client object, passing a handler to the
> constructor. The handler is an instance of a class that contains event
> handler methods implemented by the user. (The expected interface of a
> handler is defined in an abstract base class.)
> Afterwards the user calls run() on the client to start processing.
>
> Problem:
> So far this works, but only for applications that solely react to events
> from the network. Now I want to be able to use this library for network
> communication in interactive applications (command line or GUI), too. In
> this case it needs to be able to respond to user input, which must be
> somhow incorporated in the event loop. GUI libraries like PyQt even
> provide their own event loop.
>
> Questions:
> So how do I integrate those aspects in my library?
> Is asyncio the right choice?
> Or does my whole approach goes in the wrong direction?
>
> I would greatly appreciate any help and suggestions,
>
> Tobias
>
>
> [1]
> The network protocol is IRC (Internet Relay Chat) but I think that does
> not really matter here.
>

I'm sorry that I can't help directly, but the asyncio module is so new 
that I don't know if any of the other regulars here can help either :( 
I'd be inclined to wait for 24 hours from time of posting and if you 
don't get any positive responses, then ask again on the main Python 
mailing list/news group.  Note that personally I would not ask on 
stackoverflow, I've seen too many completely wrong answers there top voted.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence



More information about the Tutor mailing list