print function does not get called in an async function before await in python
10,699
coroutines don't get executed until they are awaited, that is why it's happening
Author by
Sens4
Updated on July 26, 2022Comments
-
Sens4 almost 2 years
I want to get some links with requests and asyncio.I have a sample program but I think there is a problem because the print function only gets called when I'm using await.
So why doesn't print gets called where I call the actual function? What I have understood if the await keyword is used, the function interrupts until the future is presentable. In my case, the print function should get called before the await keyword so before the print statement:
doing stuff in between
or am I wrong?import asyncio import requests import bs4 urls = ["http://www.google.com", "http://www.google.co.uk"] async def getContent(url): loop = asyncio.get_event_loop() print("getting content for: " + url) # print should be called here # execute a non async function async future = loop.run_in_executor(None, requests.get, url) # doing stuff with bs4 soup = bs4.BeautifulSoup((await future).text, "html.parser") # should now interrupt return soup async def main(): loop = asyncio.get_event_loop() print("starting gathering...") # creating a list of futures futures = [getContent(url) for url in urls] # packing futures into a awaitable list future responses_future = asyncio.gather(*futures) print("doing stuff in between...") # waiting for all futures responses = await responses_future print("Done") for response in responses: print(response) loop = asyncio.get_event_loop() loop.run_until_complete(main())
output:
starting gathering... doing stuff in between... getting content for: http://www.google.com getting content for: http://www.google.co.uk Done HTML CODE HERE!
Regards