Skip to content

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

Closed
@fried

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions