QUERY · ISSUE
asyncio implement cpython all_tasks
enhancement
It would be very useful to have the ability to get a list of all tasks similar to the cpython implementation.
Currently to track all running tasks, tasks must be stored in a global list variable which is not ideal and error prone.
- Would you be willing to sponsor this work? Yes
CANDIDATE · PULL REQUEST
extmod/asyncio: Add `Task` methods from CPython
extmod
This updates the way that Task is defined in the C module for asyncio which implements tasks & task queues. The goal is to bring asyncio in MicroPython slightly closer to CPython.
This adds the following methods to Task:
get_coro()- how CPython exposes the coroutine backing the task (CPython Docs)result()- a helper method from CPython for returning the successful response (CPython Docs)exception()- a helper method from CPython for returning the raised values (CPython Docs)cancelled()- helper for true / false if the task has been cancelled (CPython Docs)add_done_callback()- adds a callback to the task for completion or if already complete executes it (CPython docs)remove_done_callback()- removes a specific "done" callback from the task (CPython docs)
~Adds a hash unary operation so Tasks can be added to sets like in CPython.~
~Also adds set_result and set_exception for consistency but neither of these do anything - they raise a RuntimeError because they aren't supported on Tasks. :shrug:~