Created
August 15, 2025 18:16
-
-
Save rmsaitam/d3644d999ed1fb2c229ae6d087c16949 to your computer and use it in GitHub Desktop.
Exemplo de programação assíncrona no Python
This file contains hidden or 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 | |
| async def fetch(url): | |
| async with aiohttp.ClientSession() as session: | |
| async with session.get(url) as response: | |
| print(f"Baixando {url}...") | |
| html = await response.text() | |
| print(f"Concluído: {url} ({len(html)} caracteres)") | |
| return html | |
| async def main(): | |
| urls = [ | |
| "https://example.com", | |
| "https://httpbin.org/get", | |
| "https://python.org" | |
| ] | |
| # Executa todas as requisições ao mesmo tempo | |
| resultados = await asyncio.gather(*(fetch(url) for url in urls)) | |
| print("\nDownloads finalizados!") | |
| # Executar o programa | |
| asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment