Skip to content

Instantly share code, notes, and snippets.

@Hermann-SW
Created November 20, 2018 23:00
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 Hermann-SW/6cfd38703614b0f6b4f5e19dcbf4e8a9 to your computer and use it in GitHub Desktop.
Save Hermann-SW/6cfd38703614b0f6b4f5e19dcbf4e8a9 to your computer and use it in GitHub Desktop.
65 concurrent uasyncio worker threads on 28KB free RAM ESP-01s module MicroPython
import uasyncio as asyncio
N = 65
loop = asyncio.get_event_loop(runq_len = N+1)
def collatz(x):
yield x
while x!=1:
x = 3*x+1 if x%2==1 else x//2
yield x
async def worker(x):
global N
c = 0
for k in collatz(x):
c = c+1
await asyncio.sleep(0)
print(x, c-1)
N = N-1
class Waiting():
def __await__(self):
global N
while N>0: yield from asyncio.sleep(1)
__iter__ = __await__ # issue 2678
async def killer():
await Waiting()
trash = [ loop.create_task(worker(N-x)) for x in range(N) ]
loop.run_until_complete( killer() )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment