-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
extmod/uasyncio: Provide asyncio.iscoroutine function. #11526
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Code size report:
|
Matches https://docs.python.org/3/library/asyncio-task.html#asyncio.iscoroutine Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
3bce440
to
d4904f1
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #11526 +/- ##
=======================================
Coverage 98.39% 98.39%
=======================================
Files 156 156
Lines 20609 20609
=======================================
Hits 20278 20278
Misses 331 331 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
I don't know if this is any help but I have classes where a passed callback may be either a function or a coroutine. The detection is as follows: async def _g():
pass
type_coro = type(_g())
# If a callback is passed, run it and return.
# If a coro is passed initiate it and return.
def launch(func, tup_args):
res = func(*tup_args)
if isinstance(res, type_coro):
res = asyncio.create_task(res)
return res |
@peterhinch thanks yeah I was testing doing exactly the same thing, unfortunately it's not cpython compatible - there the That being said, this type() based method does work for implementing |
Would be a good addition. I've run into this as well, and use Peter's method. I strive for interoperability with python code, having same modules running on server and embedded. Anything along that path, especially implementing an existing standard method, sounds good to me. |
There's |
? I can't find it in the inspect source. |
Ugh, sorry, you're right, that was my own copy with
added. Seems I forgot to create a PR for that. |
@smurfix fwiw I would certainly support a PR for that though, I'd be quite happy to have this as an optional install via mip as part of the inspect library. |
I added a comment to that effect to micropython/micropython-lib#998 and will create a separate PR if that's merged without these two lines. |
Closed in favour of micropython/micropython-lib#998 thanks @smurfix ! |
Matches https://docs.python.org/3/library/asyncio-task.html#asyncio.iscoroutine
I've run into needing this function a number of times when using cpython compatible asyncio code, particularly with code that passes (async) functions around like "function pointers" and/or wrapper functions.
When a non-async function is used instead of an async one, the
await function()
fails with errors likeTypeError: 'NoneType' object isn't iterable
(assuming function returns None) which can be tricky to track and debug. In these cases I'd like to do anasyncio.iscoroutine(function)
earlier to ensure the right function type has been passed in, preferably in a manner that's compatible with cpython.