Created
November 9, 2020 08:28
-
-
Save hqhs/ef8a3b36199bb3ab19863db2faddebbf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import aiohttp | |
from time import monotonic | |
from random import randrange | |
async def send_request_to_expona(session): | |
i = (randrange(1000) + 50)/1000 | |
print('waiting: {}', i) | |
await asyncio.sleep(i) | |
async def wait_first(): | |
async with aiohttp.ClientSession() as session: | |
done, pending = await asyncio.wait( | |
[send_request_to_expona(session) for _ in range(3)], | |
return_when=asyncio.FIRST_COMPLETED) | |
print("Result", done.pop().result()) | |
if __name__=='__main__': | |
start = monotonic() | |
asyncio.get_event_loop().run_until_complete(wait_first()) | |
print(f'WorkTime: {monotonic() - start}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment