Я создал asyncio event_loop в главном потоке и передал его в качестве аргумента потоку asyncio. Теперь Tkinter не будет зависать, пока получаются URL-адреса.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
def _asyncio_thread(async_loop): async_loop.run_until_complete(do_urls()) def do_tasks(async_loop): """ Button-Event-Handler starting the asyncio part. """ threading.Thread(target=_asyncio_thread, args=(async_loop,)).start() async def one_url(url): """ One task. """ sec = random.randint(1, 8) await asyncio.sleep(sec) return 'url: {}\tsec: {}'.format(url, sec) async def do_urls(): """ Creating and starting 10 tasks. """ tasks = [one_url(url) for url in range(10)] completed, pending = await asyncio.wait(tasks) results = [task.result() for task in completed] print('\n'.join(results)) def do_freezed(): messagebox.showinfo(message='Tkinter is reacting.') def main(async_loop): root = Tk() Button(master=root, text='Asyncio Tasks', command= lambda:do_tasks(async_loop)).pack() buttonX = Button(master=root, text='Freezed???', command=do_freezed).pack() root.mainloop() if __name__ == '__main__': async_loop = asyncio.get_event_loop() main(async_loop) |