Using asyncio in event-driven network library

Chris Angelico rosuav at gmail.com
Mon Dec 23 17:47:16 CET 2013


On Mon, Dec 23, 2013 at 10:50 PM, Tobias M. <tm at tobix.eu> wrote:
> 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 .

As was acknowledged on -tutor, asyncio is extremely new. I don't know
if anyone's yet used it for GUI handling, or even if the module was
intended for that.

You may be able to wrap your GUI code up so it fits inside asyncio.
Rather than run a simple event loop, you could have your own loop that
does some kind of checking: PyGTK has functions for peeking at events
and doing just one iteration of the main loop, so you could pump all
events with something like this:

while gtk.events_pending(): gtk.main_iteration()

No doubt other toolkits have something similar.

Alternatively, you could run two threads: one for your GUI, and
another one for your asyncio. How easy and clean that would be is hard
to say without trying it; it could work out beautifully for your code,
or it could be horribly clunky.

This might be your big contribution to Python for the year: a nice,
easy, working example of using asyncio with a GUI toolkit! You might
be the first to look for this, but you won't be the last.

ChrisA



More information about the Python-list mailing list