Skip to content

Instantly share code, notes, and snippets.

@cefn
Created January 6, 2018 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cefn/e32496544afd644749d9f89f4b59b6e3 to your computer and use it in GitHub Desktop.
Save cefn/e32496544afd644749d9f89f4b59b6e3 to your computer and use it in GitHub Desktop.
Checking how Micropython async generators work.
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