Код: Выделить всё
import asyncio
import time
import requests
async def get_text(url):
print(f"Load {url}")
data = requests.get(url).text
await asyncio.sleep(0.0001)
print(f"Finished loading {url}")
return data
async def main(name):
tasks = [loop.create_task(get_text(n)) for n in name]
await asyncio.wait(tasks)
return tasks
if __name__ == '__main__':
url = ['https://www.nytimes.com', 'https://news.yahoo.com']
s = time.perf_counter()
loop = asyncio.get_event_loop()
result = loop.run_until_complete(main(url))
loop.close()
e = time.perf_counter() - s
print(f"Time takes: {e:0.2f}s")
Подробнее здесь: https://stackoverflow.com/questions/610 ... ed-benefit
Мобильная версия