0

Using async await makes the methods execute on the UI thread by default. I have ran into situations where performing await on an expensive operation will result in the UI blocking (Like this post )

For example,

import asyncio

async def freeze_ui_5_seconds():
    await asyncio.sleep(5)

awaiting this function on the UI thread will freeze the UI for 5 seconds. By utilizing await the code will pause and not continue until the await method has completed and a value can be returned. My question is, what does this achieve in the end if await essentially makes the code synchronous, that is, we will not continue until the await method has completed. Using await in my mind just retracts the idea of asynchronous programming as we are now "pausing" and "not continuing" until this method has completed and will freeze the UI.

J. Doe
  • 92
  • 5
  • A simple example is that you can invoke several async function in a row, and they will not wait for the previous ones to finish – MMZK1526 Aug 11 '22 at 03:03
  • 1
    You can create several async tasks that don't wait for each other, but use `asyncio.gather` to wait for all of them to finish. https://docs.python.org/3/library/asyncio-task.html#asyncio.gather – qrsngky Aug 11 '22 at 03:09
  • 1
    This can be solved by coupling the GUI event loop and the async event loop. E. g. for tkinter it is described in https://stackoverflow.com/questions/47895765/use-asyncio-and-tkinter-or-another-gui-lib-together-without-freezing-the-gui. – Michael Butscher Aug 11 '22 at 03:13
  • @MMZK1526 if I had several async functions using `await`, I would need to wait for one to finish before it moved on. – J. Doe Aug 11 '22 at 05:29

0 Answers0