Skip to content

Instantly share code, notes, and snippets.

@leotada
Created July 1, 2022 21:57
Show Gist options
  • Save leotada/5f4f31c4d228aac598625c4ae7287891 to your computer and use it in GitHub Desktop.
Save leotada/5f4f31c4d228aac598625c4ae7287891 to your computer and use it in GitHub Desktop.
python async vs sync
import asyncio
from time import time
import httpx
url = 'https://api.coindesk.com/v1/bpi/currentprice.json'
async def busca_bitcoin():
async with httpx.AsyncClient() as client:
print((await client.get(url)).text)
async def main(lento):
tasks = []
for i in range(10):
tasks.append(busca_bitcoin())
inicio = time()
if lento:
for task in tasks:
await task
else:
await asyncio.gather(*tasks)
print('Time:', time()-inicio)
if __name__ == '__main__':
asyncio.run(main(lento=True))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment