Asyncio - function type

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
DavidRKnowles
Posts: 15
Joined: Mon Sep 23, 2019 10:11 am

Asyncio - function type

Post by DavidRKnowles » Wed Oct 14, 2020 12:24 pm

Hi everyone, I am stumped on this project that uses a task to call driver functions based on commands sent via mqtt.

The part that is a pain is the drivers contain some asynchronous functions and some standard functions. I need the task that calls these functions to be able to identify the Async functions so I can call them with "await" and then just call the others normally.

I can't seem to identify the function in a condition, while it says the Async function is a "generator" I can seem to check this class type in a condition.

Any ideas would be much appreciated.

DavidRKnowles
Posts: 15
Joined: Mon Sep 23, 2019 10:11 am

Re: Asyncio - function type

Post by DavidRKnowles » Wed Oct 14, 2020 11:17 pm

I see

Code: Select all

asyncio.iscoroutine()
and

Code: Select all

asyncio.iscoroutinefunction()
are not used in uasyncio, is there another way to query a generator object?

DavidRKnowles
Posts: 15
Joined: Mon Sep 23, 2019 10:11 am

Re: Asyncio - function type

Post by DavidRKnowles » Thu Oct 15, 2020 12:30 am

The only thing I can think of seems a bit of a hack:

Code: Select all

def iscoroutine(func):
    async def gen():
        print('generator')
    return type(gen) == type(func)
This works but it would be cool if the was a native uasyncio function like asyncio.iscoroutine() that was implemented.

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

Re: Asyncio - function type

Post by pythoncoder » Thu Oct 15, 2020 9:23 am

See this code which should help. It offers a function launch(callable, args). If callable is a synchronous function it is called, if it is a coroutine it is converted to a Task and run.
Peter Hinch
Index to my micropython libraries.

DavidRKnowles
Posts: 15
Joined: Mon Sep 23, 2019 10:11 am

Re: Asyncio - function type

Post by DavidRKnowles » Thu Oct 15, 2020 12:16 pm

Thanks Peter, I sailed right over that when I was looking through your docs, nice. I have it loaded in because I was going to use your scheduling module, started writing my own then came across yours and its far superior. I just need to write a small driver to interact with my main system, enabling me to create/edit/remove scheduled items over mqtt or http.

Its finally falling together, asyncio is not as hard as I thought it would be to wrap my mind around, especially after reading your docs.

Thanks again

Post Reply