Alan Cristhian (@AlanCristhian on Github) fixed the hang-on-exit bug
by turning the asyncio event loop thread into a daemon; see
guievents.py, method GuiEventLoop._start_io_event_loop.

With this, the tkapp.py from Dino Viehland's PyCon 2013 time is ready
for prime time again!

As I said before, this example is outstanding because it demonstrates
three things:

- implementing an alternative asyncio.BaseEventLoop subclass on top of
the Tkinter event loop;
- leveraging futures and yield from to escape "callback hell";
- applying the asyncio abstractions to GUI programming instead of
network programming.

Besides updating the code so it runs on Python 3.4 with the standard
asyncio module, I added the tkapp2.py which makes it easier to run the
three demonstrations from Viehland's talk: sequential processing
(freezing the GUI), asynchronous with callbacks and asynchronous with
coroutines.

Thank you Guido and Alan for helping me with this, and Dino for the
awesome talk and example!

Victor: I made a pull request so you can merge this with the
haypo/asyncio_staging repo on Bitbucket.

Thanks!

Best,

Luciano

On Sun, Jan 25, 2015 at 10:39 PM, Luciano Ramalho <[email protected]> wrote:
> I am still stuck with this, and it will be such an awesome example
> when it works.
>
> Right now the tkapp.py app works as advertised but hangs on exit. When
> you click the Quit button the window closes but the program never
> terminates. If you hit CTRL-C the code is invariably parked in that
> lock.acquire(block, timeout) call in threading.py.
>
> Meanwhile I wrote tkapp2.py implementing the three approaches for the
> expensive processing function (sequential, nested callback and
> coroutine), with a button for each. This is nice for doing demos. But
> this second version has the same problem as the previous one: hangs on
> exit.
>
> Best,
>
> Luciano
>
>
> On Tue, Jan 20, 2015 at 7:29 PM, Luciano Ramalho <[email protected]> wrote:
>> On Tue, Jan 20, 2015 at 2:38 AM, Guido van Rossum <[email protected]> wrote:
>>> Thank you. I have not found the time to work on this further myself, so I'd
>>> like to hear how you fare!
>>
>> Hello, I made the example work: the essential Count Words button works
>> as advertised (as long as you have the holmes.txt file available).
>>
>> After applying Guido's patch from 2014-05-17, I had to add touch only
>> three lines to make guievents.GuiEventLoop subclass BaseEventLoop
>> (previously it subclassed AbstractEventLoop). See changes in [1]
>>
>> [1] 
>> https://bitbucket.org/ramalho/asyncio_staging/commits/8e7668cbd3e28c41f009e48800c9671e227b80e2
>>
>> But there is still a bug I don't know how to fix: when I click quit
>> the Tk window closes by the program hangs. It's a threading issue (see
>> output and traceback at the end of this message).
>>
>> I am now writing a tkapp2.py demo with three buttons, to show the
>> three different ways of counting words: sequentially (blocking), using
>> callbacks and coroutines.
>>
>> I'd appreciate help with the threading issue on shutdown. I did some
>> research on the Web but did not find a solution yet.
>>
>> Best,
>>
>> Luciano
>>
>> ##################################### output of running tkapp.py
>> $ python3 tkapp.py
>> Starting on thread -1601961560
>> Loading words on  -1601961560
>> 110678 words loaded
>> Cleaning on  -1601961560
>> 107396 remain after cleaning
>> Counting on -1601961560
>> 7947 distinct words after counting
>> Sorting on thread  -1601961560
>> The ten most common words: the, and, i, to, of, a, in, that, it, you
>> ^CException ignored in: <module 'threading' from
>> '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/threading.py'>
>> Traceback (most recent call last):
>>   File 
>> "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/threading.py",
>> line 1295, in _shutdown
>>     t.join()
>>   File 
>> "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/threading.py",
>> line 1061, in join
>>     self._wait_for_tstate_lock()
>>   File 
>> "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/threading.py",
>> line 1077, in _wait_for_tstate_lock
>>     elif lock.acquire(block, timeout):
>> KeyboardInterrupt
>
>
>
> --
> Luciano Ramalho
> Twitter: @ramalhoorg
>
> Professor em: http://python.pro.br
> Twitter: @pythonprobr



-- 
Luciano Ramalho
|  Author of Fluent Python (O'Reilly, 2015)
|     http://shop.oreilly.com/product/0636920032519.do
|  Professor em: http://python.pro.br
|  Twitter: @ramalhoorg

Reply via email to