Skip to content

Instantly share code, notes, and snippets.

@fabiocerqueira
Last active December 29, 2016 12:29
Show Gist options
  • Save fabiocerqueira/3acd6ae002160aadc7b641a8f497fc91 to your computer and use it in GitHub Desktop.
Save fabiocerqueira/3acd6ae002160aadc7b641a8f497fc91 to your computer and use it in GitHub Desktop.
import asyncio
import random
async def slow_action():
await asyncio.sleep(random.randrange(1, 5))
async def my_range(n):
i = 0
while i < n:
await slow_action()
yield i
i += 1
async def example(task_id, n):
async for i in my_range(n):
print(f'[{task_id}] async for {i}')
if __name__ == '__main__':
loop = asyncio.get_event_loop()
tasks = [loop.create_task(example(i, 10)) for i in range(4)]
try:
app = loop.run_until_complete(
asyncio.wait(tasks)
)
finally:
loop.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment