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

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -