[Python-ideas] How the heck does async/await work in Python 3.5

Maxime S maxischmeii at gmail.com
Sat Feb 27 04:42:18 EST 2016


2016-02-27 9:08 GMT+01:00 Terry Reedy <tjreedy at udel.edu>:

> On 2/26/2016 5:12 PM, Maxime S wrote:
>
>>
>> 2016-02-25 17:47 GMT+01:00 Guido van Rossum
>> <guido at python.org
>> <mailto:guido at python.org>>:
>>
>>     When you're implementing this API on top of tkinter, you'll probably
>>     find that you'll have to use tkinter's way of sleeping anyways, so the
>>     implementation of waiting in BaseEventLoop using a selector is not
>>     useful for this scenario.
>>
>>     There are probably some possible refactorings in the asyncio package
>>     to help you reuse a little more code, but all in all I still think it
>>     would be very useful to have an asyncio loop integrated with Tkinter.
>>     (Of course Tkinter does support network I/O, so it would be possible
>>     to integrate with that, too. Or some hybrid where you somehow figure
>>     out how to wait using a Selector *or* tkinter events in the same
>>     loop.)
>>
>>
>> It is actually quite easy to implement an asyncio loop over tkinter once
>> you realise that tkapp.dooneevent() is very similar to poll(), and
>>
>
> It took me awhile to understand what you mean by 'tkapp'.  Instances of
> tkinter.Tk get an undocumented .tk attribute that is an instance of the
> undocumented and hidden _tkinter class that is called 'tkapp' in printed
> representations.  In other words, after
>
>
I actually meant an instance of Tk(), which also have dooneevent(). Sorry
this wasn't clear.



> import tkinter as tk
> root = tk.Tk()
> tkapp = root.tk
>
> tkapp has a dooneevent method.  I found the tcl doc for it at
> https://www.tcl.tk/man/tcl/TclLib/DoOneEvent.htm
> Does calling it with DONT_WAIT "TCL_DONT_WAIT - Do not sleep: process only
> events that are ready at the time of the call." differ from calling
> root.update?
>
>
Good point. The code under update() is essentially this (plus some error
cheking):

if (nargs == 1) {
    flags = TCL_DONT_WAIT;
} else {
    flags = TCL_IDLE_EVENTS;
}

while (Tcl_DoOneEvent(flags) != 0) {}

So, it is probably much more efficent to call update() than to do the same
loop in python as I did, and it avoid messing around with _tkinter
undocumented flags.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160227/56903469/attachment-0001.html>


More information about the Python-ideas mailing list