Description
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:
There was an in person meeting between @gvanrossum, @itamaro and myself where we discussed the plausibility of this feature request.
Linked PRs
- gh-119333: Add c-api to have contextvar enter/exit callbacks #119335
- gh-119333: PyContext watchers notify get interp from tstate #124444
- gh-124872: Change PyContext_WatchCallback to take PyObject #124737
- gh-124872: Back up exception before calling PyContext_WatchCallback #124741
- gh-124872: Refine contextvars documentation #124773
- gh-124872: Move PyThreadState to first argument for consistency #124774
- gh-124872: Replace enter/exit events with "switched" #124776