[Python-Dev] Adding a threadlocal to the Python interpreter (original) (raw)

Nick Coghlan ncoghlan at gmail.com
Wed May 18 22:30:27 EDT 2016


On 18 May 2016 at 23:20, Daniel Holth <dholth at gmail.com> wrote:

I would like to take another stab at adding a threadlocal "str(bytes) raises an exception" to the Python interpreter, but I had a very hard time understanding both how to add a threadlocal value to either the interpreter state or the threadlocal dict that is part of that state, and then how to access the same value from both Python and CPython code. The structs were there but it was just hard to understand. Can someone explain it to me?

Christian covered the C aspects of the API, while the general purpose Python aspects live in the threading module.

However, the Python level thread-local API doesn't provide direct access to the thread state dict. Instead, it provides access to subdicts stored under per-object keys in that dict, keyed as "thread.local.":

Getting access to state stored that way from C is possible, but significantly less convenient than access the thread state directly.

What that means is that any time we want to expose thread local state to both C and Python code, it will generally be the responsibility of the C code to both manage the key in the thread state dict (or the field in the thread state struct), and also to provide a Python API for access that state.

Cheers, Nick.

-- Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia



More information about the Python-Dev mailing list