asynchronous - print function does not get called in an async function before await in python -
i want links requests , asyncio.i have sample program think there problem because print function gets called when i'm using await.
so why doesn't print gets called call actual function? have understood if await keyword used, function interrupts until future presentable. in case, print function should called before await keyword before print statement: doing stuff in between
or 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 called here # execute non async function async future = loop.run_in_executor(none, requests.get, url) # doing stuff bs4 soup = bs4.beautifulsoup((await future).text, "html.parser") # should interrupt return soup async def main(): loop = asyncio.get_event_loop() print("starting gathering...") # creating list of futures futures = [getcontent(url) url in urls] # packing futures awaitable list future responses_future = asyncio.gather(*futures) print("doing stuff in between...") # waiting futures responses = await responses_future print("done") 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
coroutines don't executed until awaited, why it's happening
Comments
Post a Comment