Skip to content

Instantly share code, notes, and snippets.

@dbtorrico
Created June 19, 2019 19:05
Show Gist options
  • Save dbtorrico/381cd3608b1da6de69e75bec37be6bcc to your computer and use it in GitHub Desktop.
Save dbtorrico/381cd3608b1da6de69e75bec37be6bcc to your computer and use it in GitHub Desktop.
import time
import asyncio
start = time.time()
def tic():
return 'at %1.11f segundos' % (time.time() - start)
@asyncio.coroutine
def gr1():
# Demora a ser executada, mas não queremos esperar
print('gr1 iniciou a execução: {}'.format(tic()))
yield from asyncio.sleep(2)
print('gr1 terminou a execução: {}'.format(tic()))
@asyncio.coroutine
def gr2():
# Demora a ser executada, mas não queremos esperar
print('gr2 iniciou a execução: {}'.format(tic()))
yield from asyncio.sleep(2)
print('gr2 terminou a execução: {}'.format(tic()))
@asyncio.coroutine
def gr3():
print('Executando enquanto as outras estão bloqueadas: {}'.format(tic()))
yield from asyncio.sleep(5)
print('Pronto!')
ioloop = asyncio.get_event_loop()
tasks = [
ioloop.create_task(gr1()),
ioloop.create_task(gr2()),
ioloop.create_task(gr3())
]
ioloop.run_until_complete(asyncio.wait(tasks))
ioloop.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment