This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author leezu
Recipients leezu, rhettinger, yselivanov
Date 2020-02-20.23:40:40
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1582242040.82.0.373221291672.issue39660@roundup.psfhosted.org>
In-reply-to
Content
> No, but there's C API that you can use to get/set contextvars. If a C library
> is hard coded to use threadlocals I'm afraid there's nothing we can do about
> it except fixing their C code to make state storage pluggable.

I agree the C library could check the Python state. On the other hand, the Python frontend may be just one of multiple supported language frontends of the library. So it seems preferable if Python could tell the library when to change state.

> What library do you have in mind?

MXNet has such a thread-local C API to enable / disable recording for autograd. https://github.com/apache/incubator-mxnet/blob/9b38df0622fec677e6b4891dfc4c10295359996d/include/mxnet/c_api.h#L1260-L1266

> Theoretically yes, for debug purposes at least. But I still fail to see how
> you could use that API even if it existed.

Consider we are in "context 1". When we change the state in the C library by calling `old_state = lib.set_state(new_state)`, we get to know the prior state. When switching to "context 2", `lib.set_state(old_state)` should be called. When switching back to "context 1", `lib.set_state(new_state)` should be called again. It's analogous to contextvars.

Would there be too much overhead if allowing specification of a python function that contextvars calls on context changes?
History
Date User Action Args
2020-02-20 23:40:40leezusetrecipients: + leezu, rhettinger, yselivanov
2020-02-20 23:40:40leezusetmessageid: <1582242040.82.0.373221291672.issue39660@roundup.psfhosted.org>
2020-02-20 23:40:40leezulinkissue39660 messages
2020-02-20 23:40:40leezucreate