Archive

Posts Tagged ‘python’

Python asyncio and tkinter Together with TCP and UDP

February 3rd, 2017 No comments

I finally figured out at least one way to have a Python tkinter app use asyncio event loops, so I thought I should share it. In the end, the solution was simply running an asyncio event loop on another thread. I hope one day Python will merge the two event loops, but for now this works OK.

I have this on pastebin also:

The key points are extracted here:

        # Thread that will handle io loop
        def _run(loop):
            asyncio.set_event_loop(loop)
            loop.run_forever()

        ioloop = asyncio.new_event_loop()
        asyncio.run_coroutine_threadsafe(_connect(), loop=ioloop)  # Schedules connection
        t = threading.Thread(target=partial(_run, ioloop))
        t.daemon = True  # won't hang app when it closes
        t.start()  # Server will connect now

Read more…

Categories: Utility Tags: , , ,