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.22:23:29
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1582237409.79.0.805661860096.issue39660@roundup.psfhosted.org>
In-reply-to
Content
Consider a `lib = ctypes.CDLL(lib_path, ctypes.RTLD_LOCAL)` which provides APIs `lib.compute`, `lib.set_state` and a Python interface wrapping both in `def compute`, `def set_state`. Let's assume `lib.set_state` sets a thread-local state in the library implementation and further  that the behaviour of `lib.compute` depends on the last call to `lib.set_state`. 

Users of the Python interface may call `set_state` and `compute` concurrently in coroutines. Thereby one coroutine may change the state in the library, prior to another coroutine calling `compute`. This leads to a wrong result.

If `set_state` and `compute` were pure python functions, `contextvars` could be used to implement set_state and provide context-local state.

Is there any existing API that can be used to call `lib.set_state` on context changes? If so, please let me know. If not, would it make sense to extend `contexvars` to allow users to configure that `lib.set_state` is called on context change?
History
Date User Action Args
2020-02-20 22:23:29leezusetrecipients: + leezu, rhettinger, yselivanov
2020-02-20 22:23:29leezusetmessageid: <1582237409.79.0.805661860096.issue39660@roundup.psfhosted.org>
2020-02-20 22:23:29leezulinkissue39660 messages
2020-02-20 22:23:29leezucreate