Store current task on the loop

Currently the current task of a loop in stored in a global dict mapping loops to their corresponding current tasks.

However this will perform poorly in free threading build as such I propose to store it in on the loop itself.

  • asyncio._enter_task will set the task on the loop as _current_task
  • asyncio._leave_task will set it to None if the task matches otherwise it’ll raise error
  • asyncio.current_task will just lookup the attribute on loop._current_task and return it

Note that there isn’t any compatibility issue here because third party tasks already need to call _enter_task/_leave_task so it is just a change of where the data is stored.

1 Like