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.
Asyncio - function type
-
- Posts: 15
- Joined: Mon Sep 23, 2019 10:11 am
Re: Asyncio - function type
I see and are not used in uasyncio, is there another way to query a generator object?
Code: Select all
asyncio.iscoroutine()
Code: Select all
asyncio.iscoroutinefunction()
-
- Posts: 15
- Joined: Mon Sep 23, 2019 10:11 am
Re: Asyncio - function type
The only thing I can think of seems a bit of a hack:
This works but it would be cool if the was a native uasyncio function like asyncio.iscoroutine() that was implemented.
Code: Select all
def iscoroutine(func):
async def gen():
print('generator')
return type(gen) == type(func)
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Asyncio - function type
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.
Index to my micropython libraries.
-
- Posts: 15
- Joined: Mon Sep 23, 2019 10:11 am
Re: Asyncio - function type
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
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