Using asyncio in event-driven network library

Chris Angelico rosuav at gmail.com
Tue Dec 24 12:30:02 EST 2013


On Wed, Dec 25, 2013 at 2:41 AM, Tobias M. <tm at tobix.eu> wrote:
> On 23.12.2013 17:47, Chris Angelico wrote:
>> while gtk.events_pending(): gtk.main_iteration()
>
> On 23.12.2013 20:59, Terry Reedy wrote:
>>
>> I think tk(inter) has 'run pending events' or something.
>
> I didn't know functions like this exist in gui toolkits. Am I right in
> thinking that using these functions you don't have to use the built-in event
> loop. Instead you can write your own loop and just call 'run pending events'
> continuously?

That's correct. And if you do the blocking version of the call (not
shown above), then it's exactly what the main loop function is doing.

> I think what I actually want is the other way around. I want my library to
> be easy to use in existing gui applications that already have their own main
> loop. Their core should not need to be adapted for this. So I think I should
> provide a function like 'run pending events' in my api. For example in a
> PyQt application this could be called periodically by using a QTimer.

That would work if you're okay with, effectively, polling for events.
Might have a lot of cost in terms of performance (checking frequently)
or response time (checking infrequently). I'd like to see the
two-thread form explored; that effectively lets you block on two
different events. You still get the advantage of asyncio (you don't
need millions of threads - there's exactly two, one for GUI and one
for everything else), but you're forced to think about your GUI first
and give it the main thread. (At least, you are with several toolkits,
which seem to insist on being used on the main thread only. You might
be able to spin that off to another thread.)

ChrisA



More information about the Python-list mailing list