Skip to content

Instantly share code, notes, and snippets.

@rougeth
Created April 21, 2018 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rougeth/5566c3873d8e5efd488e73c396783451 to your computer and use it in GitHub Desktop.
Save rougeth/5566c3873d8e5efd488e73c396783451 to your computer and use it in GitHub Desktop.
Async que se faz - TDC 2018
# Código usado na palestra "Async que se faz - asyncio na prática"
# no TDC 2018 em Florianópolis
# Creative Commons Attribution 4.0 International license <https://creativecommons.org/licenses/by/4.0/>
import asyncio
import random
from time import sleep
from utils import UPDATE_AVG, async_request, timeit
async def buscar_pedidos_no_banco():
print('- Buscar pedidos no banco')
return range(18000)
async def checar_pedido_no_correios(pedido):
await async_request(pedido, 'Correios')
async def atualizar_pacote_no_banco(pedido):
print('- {}: Atualiza pacote'.format(pedido))
async def notificar_usuario(pedido):
await async_request(pedido, 'Telegram')
async def verificar_pedidos(pedido):
await checar_pedido_no_correios(pedido)
if random.choice(UPDATE_AVG):
await atualizar_pacote_no_banco(pedido)
await notificar_usuario(pedido)
async def main():
pedidos = await buscar_pedidos_no_banco()
tarefas = []
for pedido in pedidos:
tarefas.append(verificar_pedidos(pedido))
await asyncio.wait(tarefas)
loop = asyncio.get_event_loop()
loop.run_until_complete(timeit(main()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment