Skip to content

Add a new base class for loop scoped variables in asyncio #128633

Not planned
@kumaraditya303

Description

@kumaraditya303

We have several loop-scoped global variables now. Some are Python objects, but llist_node is a plain C structure.
Let's make a super-base C Extension class where we can put all these objects (both python and plain C) and inherit BaseEventLoop from it.
I personally feel that we will have more loop-scoped things like llist_node in the future; now is a good time to settle a place for them.

Originally posted by @asvetlov in #128416 (comment)

Activity

graingert

graingert commented on Jan 8, 2025

@graingert
Contributor

Could you expose these so user code can use it? I'd like to implement RunVars - which are like ContextVars but for a particular asyncio.run. currently we use WeakKeyDictionaries from loop to object

We also use WeakKeyDictionaries to map from task to the CancelScope it's in. I'd like to make one WeakKeyDictionary per event loop rather than having one global one

kumaraditya303

kumaraditya303 commented on Feb 9, 2025

@kumaraditya303
ContributorAuthor

Unfortunately we cannot do this or move current task to loop because the newly added external introspection rely on storing it on thread state.

I have another idea to fix this for free-threading I'll be posting that soon in a different issue.

cc @asvetlov

asvetlov

asvetlov commented on Feb 9, 2025

@asvetlov
Contributor

@graingert could you elaborate?
In my mind, both selector and proctor asyncio event loops could derive from an opaque base class implemented in C. This class is a container for data required for futures, tasks, etc.

It is perhaps not what you need

kumaraditya303

kumaraditya303 commented on May 16, 2025

@kumaraditya303
ContributorAuthor

The task linked list is per thread now so this isn't needed anymore, also moving data to per loop scoped variable would break the newly added external introspection so closing as won't fix.

moved this from Todo to Done in asyncioon May 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Add a new base class for loop scoped variables in asyncio · Issue #128633 · python/cpython

      Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

      Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant