Created
July 1, 2022 21:57
-
-
Save leotada/5f4f31c4d228aac598625c4ae7287891 to your computer and use it in GitHub Desktop.
python async vs sync
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 | |
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