Created
January 6, 2018 16:11
-
-
Save cefn/e32496544afd644749d9f89f4b59b6e3 to your computer and use it in GitHub Desktop.
Checking how Micropython async generators work.
This file contains 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 sys | |
if sys.implementation.name == "cpython": | |
import asyncio | |
elif sys.implementation.name == "micropython": | |
import uasyncio as asyncio | |
async def count(bound=8,delay=0.1): | |
for num in range(bound): | |
await asyncio.sleep(delay) | |
yield num | |
async def aggregate(asyncGenerator): | |
resultList = [] | |
async for num in asyncGenerator: | |
resultList.append(num) | |
return resultList | |
loop = asyncio.get_event_loop() | |
coroCount = count() | |
coroAggregate = aggregate(coroCount) | |
if sys.implementation.name == "cpython": | |
taskAggregate = loop.create_task(coroAggregate) | |
results = loop.run_until_complete(taskAggregate) | |
elif sys.implementation.name == "micropython": | |
results = loop.run_until_complete(coroAggregate) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment