general uasyncio class

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
ajie_dirgantara
Posts: 81
Joined: Fri Sep 02, 2016 9:26 am

general uasyncio class

Post by ajie_dirgantara » Thu Oct 19, 2017 3:38 am

I want to make a general purpose task creator, but when I do this, it raised error :

Code: Select all

  File "uasyncio/core.py", line 124, in run_forever
  File "uasyncio/core.py", line 88, in run_forever
TypeError: 'NoneType' object is not an iterator
here is the code :

Code: Select all


class c_taskMan(object):
	def __init__(self,init,callback,waitTime):
		self.tInit=init
		self.tCb=callback
		self.tAwait=waitTime
	async def taskCallback(self):
		self.tInit()
		while True:
			self.tCb()
			await uasyncio.sleep_ms(self.tAwait)


# test create task

value=0

def init():
 global value
 value=100

def task():
 global value
 if value>10:
  value-=1

o_task=c_taskMan(init,task,1000)
loop=uasyncio.get_event_loop()
loop.create_task(o_task.taskCallback())
loop.run_forever()


User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: general uasyncio class

Post by pythoncoder » Fri Oct 20, 2017 8:30 am

It works here on a Pyboard. I added this minor adaptation to make it terminate.

Code: Select all

import uasyncio

class c_taskMan(object):
    def __init__(self,init,callback,waitTime):
        self.tInit=init
        self.tCb=callback
        self.tAwait=waitTime
    async def taskCallback(self):
        self.tInit()
        while True:
            self.tCb()
            await uasyncio.sleep_ms(self.tAwait)

# test create task

value=0

def init():
    global value
    value=100

def task():
    global value
    if value>10:
        value-=1

async def killer():
    print('Waiting 5 seconds.')
    await uasyncio.sleep(5)
    print(value)

o_task = c_taskMan(init,task,1000)
loop = uasyncio.get_event_loop()
loop.create_task(o_task.taskCallback())
loop.run_until_complete(killer())
Peter Hinch
Index to my micropython libraries.

Post Reply