0

I'm trying to wrapping my head around asyncio. I think I'm rather confident using MultiThreads and I think that confuses me, since I keep mapping asyncio back to MultiThreads.

Say I have the following

async def get_url_data(url):
    data = await some_api_call()
    return data

as far as I understand; when we encounter await it's like saying "Run this function in the background and move on" - but some say that await "gives control back to the event loop". Does that mean that await "pauses" the function (or schedules it?) or is it, as I understood, that await makes the function run in the background? I struggle to figure out when the function is executed and the values are returned

CutePoison
  • 4,679
  • 5
  • 28
  • 63
  • 1
    A short answer would be: "tries to run the function, but is prepared to give control back to the event loop if the function indicates that it doesn't have a result yet". So it's closer to the former out of the two you specify (asyncio is strongly single-threaded). You might want to look at answers to [this question](https://stackoverflow.com/questions/49005651/how-does-asyncio-actually-work) or [this lecture](https://www.youtube.com/watch?v=7JtNiwCH_OA) which tries to explain precisely what `await` desugars to. – user4815162342 Mar 16 '21 at 14:50
  • `await` is blocking, so rather than "run this function in the background and move on," it's "don't continue on until this function has finished." The values are returned immediately upon execution of the `return` statement, which here is blocked until the completion of `some_api_call`. – dirn Mar 16 '21 at 15:09

0 Answers0