Skip to content

Add budget for asyncio Task #126684

@hidva

Description

@hidva

Feature or enhancement

Proposal:

Should we add a mechanism similar to Per-task operation budget:

Even though Tokio is not able to preempt, there is still an opportunity to nudge a task to yield back to the scheduler. As of 0.2.14, each Tokio task has an operation budget. This budget is reset when the scheduler switches to the task. Each Tokio resource (socket, timer, channel, ...) is aware of this budget. As long as the task has budget remaining, the resource operates as it did previously. Each asynchronous operation (actions that users must .await on) decrements the task's budget. Once the task is out of budget, all Tokio resources will perpetually return "not ready" until the task yields back to the scheduler. At that point, the budget is reset, and future .awaits on Tokio resources will again function normally.

The possible modifications are as follows:

diff --git a/Lib/asyncio/futures.py b/Lib/asyncio/futures.py
index 5f6fa234872..6356d9c2f6d 100644
--- a/Lib/asyncio/futures.py
+++ b/Lib/asyncio/futures.py
@@ -285,6 +285,8 @@ def __await__(self):
         if not self.done():
             self._asyncio_future_blocking = True
             yield self  # This tells Task to wait for completion.
+        if current_task_has_no_budget:
+            yield self  # We don't have a budget! Let others run.
         if not self.done():
             raise RuntimeError("await wasn't used with future")
         return self.result()  # May raise too.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions