Skip to content

Add c-api to set callback function on enter and exit of PyContext #119333

Closed

Description

@fried

Feature or enhancement

Proposal:

In python every time a context is switched, two c-api are called.

PyContext_Enter(PyObject *);
PyContext_Exit(PyObject *);

I need a way to know when those api's are called on a context. The purpose is that at Meta we have a similar system to contextvars in C++ called folly::requestcontext. When the python context switches we need to inform C++ to swap its active context. Right now we have been carry forward patches to Asyncio.Task to do this on the _step() since way back in py3.6 which is generally the wrong place to do it.

There is prior art in #91054 which provides watching creation/destruction of Code objects. The proposed solution would mirror that functionality closely.

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

https://discuss.python.org/t/request-can-we-get-a-c-api-hook-into-pycontext-enter-and-pycontext-exit/51730

There was an in person meeting between @gvanrossum, @itamaro and myself where we discussed the plausibility of this feature request.

Linked PRs

Activity

  1. added a commit that references this issue on Sep 24, 2024
  2. added a commit that references this issue on Sep 26, 2024
  3. rhansen commented on Sep 27, 2024

    @rhansen
    Contributor

    @fried I'm not sure that context entry and exit are the appropriate semantics for this. Don't you actually want to know when the "current context" switches to a different context?

    Why this matters: #99633 is proposing to add the ability to use contextvars.Context as a context manager, which involves temporarily changing the "current context" (but not necessarily entering and exiting contexts) while a generator or coroutine is executing. See #99633 (comment) for some technical details.

  4. added 9 commits that reference this issue on Sep 28, 2024
  5. 3 remaining items

  6. added 15 commits that reference this issue on Sep 30, 2024
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

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions