uasyncio/aswitch : problem cancelling coros on the throw of a switch.

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Jim.S
Posts: 79
Joined: Fri Dec 18, 2015 7:36 pm

uasyncio/aswitch : problem cancelling coros on the throw of a switch.

Post by Jim.S » Sun May 13, 2018 10:13 am

I am having problems cancelling a coro using await asyn.Cancellable.cancel_all()) from a aswitch close_func() call

I have successfully used aswitch to (a) send an event to a coro to get it to continue running (b) set a parameter to terminate a while loop, i.e.

Code: Select all

        loop=asyncio.get_event_loop()
        self.consumer_on_event=asyn.Event()
        pin11 = Pin('X11', Pin.IN, Pin.PULL_UP)
        switch11=sw.Switch(pin11)##3
        switch11.close_func(self.consumer_on_event.set)
and

Code: Select all

        pin12= Pin('X12', Pin.IN, Pin.PULL_UP)
        switch12=sw.Switch(pin12)##4
        switch12.close_func(self.set_parameter,(False,))       #set_ parameter is just a simple class method to set a boolan flag 
So I am reasonably confident I can get aswitch and uasyncio to work together nicely. I also can cancel my coros belonging to the consumer group using

Code: Select all

await asyn.Cancellable.cancel_all(group='consumer')
Now I want to use switch12 to cancel a cancellable task belonging to a group (identified by the string consumer) i.e.

Code: Select all

switch12.close_func(asyn.Cancellable.cancel_all,(group='consumer',) )
however, this line is syntactially incorrect, So I tried the following

Code: Select all

switch12.close_func(asyn.Cancellable.cancel_all(group='consumer'))
This line throws a TypeError: 'generator' object is not callable exception

What am I doing wrong?

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

Re: uasyncio/aswitch : problem cancelling coros on the throw of a switch.

Post by pythoncoder » Mon May 14, 2018 5:48 am

This is a classic red and blue function problem. The aswitch class expects a function as an argument whereas cancel_all is a coroutine. In uasyncio coroutines are implemented as generators, hence your error message. You need to do something like this

Code: Select all

async def cancellation():
    await asyn.Cancellable.cancel_all(group='consumer')

def terminator():  # This is a function
    loop = asyncio.get_event_loop()
    loop.create_task(cancellation())

switch12.close_func(terminator)
Note I haven't tested this but I'm pretty confident that this points to a solution.

Historical note. I wrote the aswitch module to use my old scheduler in the days before uasyncio was usable. When uasyncio matured I abandoned my now inferior scheduler and ported aswitch to uasyncio. I may be able to fix aswitch so that it can accept a coroutine or a function as an argument. But at present it expects a function.
Peter Hinch

Jim.S
Posts: 79
Joined: Fri Dec 18, 2015 7:36 pm

Re: uasyncio/aswitch : problem cancelling coros on the throw of a switch.

Post by Jim.S » Mon May 14, 2018 7:50 pm

Thanks Peter, I would never have been able to figure that out.

Post Reply