On 8/11/2016 2:34 AM, Christian Gollwitzer wrote:
Am 11.08.16 um 06:38 schrieb Terry Reedy:
You might be able to glean something from the succession of files I
uploaded to
https://bugs.python.org/issue27546
Integrate tkinter and asyncio (and async)
I started with just mixing tk and asyncio callbacks. After some
struggle with similar question as you ask above, I ended up with more or
less callback free code using async def and async for. Once I got over
the hump, I rather like it.
My next experiment, not uploaded, is adapting tk mainloop so it can be
used instead of asyncio.loop.runforever as the driver for async def and
async for and await. There is either a simple bug I overlooked or I am
missing some detail of how the future and task interact.
Interesting discussion! Tcl itself acquired coroutines some years ago
(in 8.6) and people in this community are very excited with async based
programming.
When I read something like "Python finally acquired an event loop in
3.4" I wonder where people have been. The tk event loop has been in
Python perhaps for 2 decades, and one actually could call select every
xx milleseconds. The main problem is that tcl/tk does not do file
events on Windows. The asyncio people did tackle that, but ended up
with two loops, Selector and Proactor, neither of which are as complete
as Selector on *nix. Tcl/tk could try to borrow than work.
I'm convinced that it is possible to integrate Tcl's event loop with
asyncio's loop without regular update polling. This might require a
patch to Tkinter at the C level. For example, an easy way is to put
Tcl/Tk in it's own thread. The Tcl event loop can be woken up from a
different thread by the TclThreadQueueEvent() and TclTheradAlert() API
calls. Basically, you would spawn a new thread (from the C level), when
loading Tkinter, which loads Tk and sits in a Tcl event loop ("vwait
forever"). tk.eval() and tk.call() would be redefined to post events
using the aforementioned functions. When a Tcl callback arrives, it
would send an event to asyncio in the main thread - no idea how this
works, though. In the end, it would also allow to use Tkinter from
different Python threads, because all calls would be rooted to the one
instance only.
--
Terry Jan Reedy
--
https://mail.python.org/mailman/listinfo/python-list