4 ms·
I'm not 100% on what's going on under the hood, but the way I think of async/await is literally as the name suggests. The code executes asynchronously, and if
by ZephyrBlu 6y ago
I'm not 100% on what's going on under the hood, but the way I think of async/await is literally as the name suggests.
The code executes asynchronously, and if there's a specific operation I want to wait for before continuing execution, I signal that I want to synchronously execute this block of code using await.
I understand this is not technically how async/await works, however I believe it's an easy way to read and understand how async code will execute.
For example:
async def func():
# do some stuff asynchronously
# wait to get values before continuing execution
values = await get_values()
# do more stuff with values asynchronously
- dnautics 6y agoThe thing is that in other languages (F#, c++, elixir) async is a directive to run a (normal or normal-looking) function asynchronously, with await being the directive for the calling function to catch the asyncd function's future.
- deleted 6y ago[deleted]
- Stratoscope 6y agoThat looks like a good way to understand it, but I think you have "synchronous" and "asynchronous" reversed. When you're running ordinary Python code, it is synchronous - it runs line by line and has control of the CPU. When you await on another function, that is the asynchronous part - your code is suspended until that other code completes. And then after the await is satisfied, your function starts to run synchronously again.