Skip to content

Instantly share code, notes, and snippets.

@taddeimania
Last active October 7, 2015 16:10
Show Gist options
  • Save taddeimania/845cd4e85c5330dcbb15 to your computer and use it in GitHub Desktop.
Save taddeimania/845cd4e85c5330dcbb15 to your computer and use it in GitHub Desktop.
New Async syntax in python 3.5 example
import asyncio
class AsyncIterator:
def __init__(self, data):
self.data = data
async def __aiter__(self):
return self
async def __anext__(self):
try:
return next(self.data)
except StopIteration:
raise StopAsyncIteration
async def hello():
a_map = map(lambda x: x * 2, [1, 2, 3, 4])
a_iter = AsyncIterator(a_map)
async for x in a_iter:
print(x)
loop = asyncio.get_event_loop()
loop.run_until_complete(hello())
loop.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment