Last active
October 7, 2015 16:10
-
-
Save taddeimania/845cd4e85c5330dcbb15 to your computer and use it in GitHub Desktop.
New Async syntax in python 3.5 example
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 | |
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