Page 1 of 1

Asyncio - function type

Posted: Wed Oct 14, 2020 12:24 pm
by DavidRKnowles
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.

Re: Asyncio - function type

Posted: Wed Oct 14, 2020 11:17 pm
by DavidRKnowles
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?

Re: Asyncio - function type

Posted: Thu Oct 15, 2020 12:30 am
by DavidRKnowles
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.

Re: Asyncio - function type

Posted: Thu Oct 15, 2020 9:23 am
by pythoncoder
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.

Re: Asyncio - function type

Posted: Thu Oct 15, 2020 12:16 pm
by DavidRKnowles
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