[issue39660] Contextvars: Optional callbacks on state change

Yury Selivanov report at bugs.python.org
Thu Feb 20 18:51:07 EST 2020


Yury Selivanov <yselivanov at gmail.com> added the comment:

> Would there be too much overhead if allowing specification of a python function that contextvars calls on context changes?

Potentially yes, especially if we allow more than one context change callback.  Allowing just one makes the API inflexible (what if you want to use two libraries from PyPI that both want to use the callback).  Allowing multiple context change callbacks leads to complicated API.

For extra context: context switches occur on every callback invocation in asyncio and there can be thousands of them per seconds (or even more). Adding any extra code to context switching code will noticeably degrade the performance.

In general, I'd suggest patching the C library to make state management customizable (like CPython allows you to customize which memory allocator to use)

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39660>
_______________________________________


More information about the Python-bugs-list mailing list