[Python-Dev] Adding a threadlocal to the Python interpreter (original) (raw)
Christian Heimes christian at python.org
Wed May 18 17:36:42 EDT 2016
- Previous message (by thread): [Python-Dev] Adding a threadlocal to the Python interpreter
- Next message (by thread): [Python-Dev] Adding a threadlocal to the Python interpreter
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 2016-05-18 15:20, Daniel Holth 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?
Python has a two important states related to threads. The PyInterpreterState contains the state of an interpreter instance: sys module, loaded modules and a couple of additional settings. Usually there is just one interpreter state in a Python process. Additional interpreter states are used to implement subinterpreters.
Each C thread, that wants to run Python code, must have a PyThreadState. The thread state contains a reference to a PyInterpreterState. Each PyThreadState has a PyObject *dict member. You can stick Python objects into the dict. The interpreter cleans up the dict when it reaps a thread.
How performance critical is your code? Does the interpreter have to check the value of the thread local frequently? In that case you should add a new member to typedef struct _ts PyThreadState in pystate.h right before /* XXX signal handlers should also be here */.
Otherwise you can simply use PyThreadState_GetDict(). It returns a Python dict object that is local to the current thread. You can simply use a fixed key like in Modules/_decimal/_decimal.c.
Christian
- Previous message (by thread): [Python-Dev] Adding a threadlocal to the Python interpreter
- Next message (by thread): [Python-Dev] Adding a threadlocal to the Python interpreter
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]