Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #62617

Using asyncio in event-driven network library

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <tm@tobix.eu>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'yet.': 0.04; 'handler': 0.05; 'irc': 0.05; 'url:pipermail': 0.05; 'problem:': 0.07; 'processing.': 0.07; 'suggestions,': 0.07; 'data):': 0.09; 'input,': 0.09; 'library?': 0.09; 'def': 0.12; 'gui': 0.12; 'posted': 0.15; 'afterwards': 0.16; 'constructor.': 0.16; 'direction?': 0.16; 'essentials': 0.16; 'event-driven': 0.16; 'handler,': 0.16; 'initializes': 0.16; 'internally': 0.16; 'loop.': 0.16; 'port))': 0.16; 'received:188.40': 0.16; 'received:188.40.28': 0.16; 'received:your-server.de': 0.16; 'subject:library': 0.16; 'url:tutor': 0.16; 'appropriate': 0.16; 'library': 0.18; 'passing': 0.19; 'server,': 0.19; 'user.': 0.19; '(the': 0.22; 'to:name:python-list@python.org': 0.22; 'header :User-Agent:1': 0.23; 'integrate': 0.24; 'questions:': 0.24; 'looks': 0.24; 'question': 0.24; 'task': 0.26; 'this:': 0.26; 'defined': 0.27; '[1]': 0.29; '[2]': 0.30; 'network.': 0.30; "i'm": 0.30; 'code': 0.31; 'too.': 0.31; 'libraries': 0.31; 'reduced': 0.31; 'class': 0.32; 'interface': 0.32; 'url:python': 0.33; 'implemented': 0.33; 'skip:_ 10': 0.34; 'but': 0.35; 'really': 0.36; 'module.': 0.36; 'object,': 0.36; 'url:org': 0.36; 'wrong': 0.37; 'list': 0.37; 'architecture': 0.38; 'expected': 0.38; 'server': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'aspects': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'mailing': 0.39; 'url:mail': 0.40; 'how': 0.40; 'even': 0.60; 'read': 0.60; 'easy': 0.60; 'new': 0.61; 'matter': 0.61; 'provide': 0.64; 'skip:a 40': 0.72; 'received:178': 0.74; 'asynchronous': 0.84; 'goal:': 0.84; 'subject:Using': 0.84; 'incorporated': 0.95
Date Mon, 23 Dec 2013 12:50:29 +0100
From "Tobias M." <tm@tobix.eu>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130804 Thunderbird/17.0.8
MIME-Version 1.0
To "python-list@python.org" <python-list@python.org>
Subject Using asyncio in event-driven network library
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Authenticated-Sender tm@tobix.eu
X-Virus-Scanned Clear (ClamAV 0.97.8/18275/Mon Dec 23 06:49:47 2013)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4548.1387799456.18130.python-list@python.org> (permalink)
Lines 63
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1387799456 news.xs4all.nl 2927 [2001:888:2000:d::a6]:39873
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:62617

Show key headers only | View raw


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 
somehow 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

PS:
I posted this question to the tutor mailing list [2] but looks like 
python-list is a better place for it.

[1] The network protocol is IRC (Internet Relay Chat) but I think that 
does not really matter here.
[2] https://mail.python.org/pipermail/tutor/2013-December/099089.html

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Using asyncio in event-driven network library "Tobias M." <tm@tobix.eu> - 2013-12-23 12:50 +0100

csiph-web