adafruit / adafruit/Adafruit_CircuitPython_asyncio
Could `TaskQueue` be implemented without `Task` being in C?
- Dominant language
- Python
- Stars
- 30
- Forks
- 16
- PR merge metrics
- No merged PRs in 30d
Description
I have a hypothesis that the big reason why `Task` and `TaskQueue` are implemented in C are because the task queue's pairing heap implementation in C is far more performant than implementing it in native python.
Because the entire TaskQueue is in C, with the way it's written it needs to reference the tasks themselves - so then the Task class has to be written in C as well. This leads to troubles when - for example - we want to reference other errors, make changes to the Task class, etc. (Okay, so maybe it's just me that has been feeling this pain!)
Something I was poking at is rewriting the `TaskQueue` to use the `heapq` implementation. The big question I have and why I opened this issue is to ask "Is this a path forward?"
Another alternative would be to abstract the `Task` piece so it no longer stores its own `ph_key` / etc and include the task within the pairing heap wrapped. There's two places where `Task`'s `ph_key` is checked outside of the task queue - once by the task itself [during cancellation](https://github.com/adafruit/Adafruit_CircuitPython_asyncio/blob/main/asyncio/task.py#L210) (this could be done by somehow getting when a task is scheduled from the currently running loop?) - and once by the [running loop](https://github.com/adafruit/Adafruit_CircuitPython_asyncio/blob/main/asyncio/core.py#L242) which could just be asking the task queue when the next Task is going to be at during the peek.
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by reading asyncio/task.py around the cancellation logic and asyncio/core.py around the running-loop lookup referenced in the issue. Compare those uses with the C-based Task and TaskQueue implementation and the proposed heapq alternative. Done would require a decided path that preserves scheduling and cancellation behavior, but the issue does not specify acceptance tests or a concrete implementation scope.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c, python
- Domain
- backend
- Issue type
- Refactor
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100