Skip to content

Instantly share code, notes, and snippets.

@hqhs
Created November 9, 2020 08:28
Show Gist options
  • Save hqhs/ef8a3b36199bb3ab19863db2faddebbf to your computer and use it in GitHub Desktop.
Save hqhs/ef8a3b36199bb3ab19863db2faddebbf to your computer and use it in GitHub Desktop.
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