Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
fried opened this issue May 21, 2024 · 0 comments
Open

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

fried opened this issue May 21, 2024 · 0 comments
Labels
topic-C-API type-feature A feature request or enhancement

Comments

@fried
Copy link
Contributor

fried commented May 21, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-C-API type-feature A feature request or enhancement
2 participants