@@ -919,8 +919,36 @@ Note that the ``PyGILState_*`` functions assume there is only one global
919
919
interpreter (created automatically by :c:func:`Py_Initialize`). Python
920
920
supports the creation of additional interpreters (using
921
921
:c:func:`Py_NewInterpreter`), but mixing multiple interpreters and the
922
- ``PyGILState_*`` API is unsupported.
922
+ ``PyGILState_*`` API is unsupported. This is because :c:func:`PyGILState_Ensure`
923
+ and similar functions default to :term:`attaching <attached thread state>` a
924
+ :term:`thread state` for the main interpreter, meaning that the thread can't safely
925
+ interact with the calling subinterpreter.
926
+
927
+ Supporting subinterpreters in non-Python threads
928
+ ------------------------------------------------
929
+
930
+ If you would like to support subinterpreters with non-Python created threads, you
931
+ must use the ``PyThreadState_*`` API instead of the traditional ``PyGILState_*``
932
+ API.
933
+
934
+ In particular, you must store the interpreter state from the calling
935
+ function and pass it to :c:func:`PyThreadState_New`, which will ensure that
936
+ the :term:`thread state` is targeting the correct interpreter::
937
+
938
+ /* The return value of PyInterpreterState_Get() from the
939
+ function that created this thread. */
940
+ PyInterpreterState *interp = ThreadData->interp;
941
+ PyThreadState *tstate = PyThreadState_New(interp);
942
+ PyThreadState_Swap(tstate);
943
+
944
+ /* GIL of the subinterpreter is now held.
945
+ Perform Python actions here. */
946
+ result = CallSomeFunction();
947
+ /* evaluate result or handle exception */
923
948
949
+ /* Destroy the thread state. No Python API allowed beyond this point. */
950
+ PyThreadState_Clear(tstate);
951
+ PyThreadState_DeleteCurrent();
924
952
925
953
.. _fork-and-threads:
926
954
@@ -1097,6 +1125,10 @@ code, or when embedding the Python interpreter:
1097
1125
.. seealso:
1098
1126
:c:func:`PyEval_ReleaseThread`
1099
1127
1128
+ .. note::
1129
+ Similar to :c:func:`PyGILState_Ensure`, this function will hang the
1130
+ thread if the runtime is finalizing.
1131
+
1100
1132
1101
1133
The following functions use thread-local storage, and are not compatible
1102
1134
with sub-interpreters:
@@ -1123,10 +1155,10 @@ with sub-interpreters:
1123
1155
When the function returns, there will be an :term:`attached thread state`
1124
1156
and the thread will be able to call arbitrary Python code. Failure is a fatal error.
1125
1157
1126
- .. note ::
1127
- Calling this function from a thread when the runtime is finalizing will
1128
- hang the thread until the program exits, even if the thread was not
1129
- created by Python. Refer to
1158
+ .. warning ::
1159
+ Calling this function when the runtime is finalizing is unsafe. Doing
1160
+ so will either hang the thread until the program ends, or fully crash
1161
+ the interpreter in rare cases. Refer to
1130
1162
:ref:`cautions-regarding-runtime-finalization` for more details.
1131
1163
1132
1164
.. versionchanged:: 3.14
@@ -1143,28 +1175,37 @@ with sub-interpreters:
1143
1175
Every call to :c:func:`PyGILState_Ensure` must be matched by a call to
1144
1176
:c:func:`PyGILState_Release` on the same thread.
1145
1177
1146
-
1147
1178
.. c:function:: PyThreadState* PyGILState_GetThisThreadState()
1148
1179
1149
1180
Get the :term:`attached thread state` for this thread. May return ``NULL`` if no
1150
1181
GILState API has been used on the current thread. Note that the main thread
1151
1182
always has such a thread-state, even if no auto-thread-state call has been
1152
1183
made on the main thread. This is mainly a helper/diagnostic function.
1153
1184
1154
- .. seealso: :c:func:`PyThreadState_Get``
1185
+ .. note::
1186
+ This function does not account for :term:`thread states <thread state>` created
1187
+ by something other than :c:func:`PyGILState_Ensure` (such as :c:func:`PyThreadState_New`).
1188
+ Prefer :c:func:`PyThreadState_Get` or :c:func:`PyThreadState_GetUnchecked`
1189
+ for most cases.
1155
1190
1191
+ .. seealso: :c:func:`PyThreadState_Get``
1156
1192
1157
1193
.. c:function:: int PyGILState_Check()
1158
1194
1159
1195
Return ``1`` if the current thread is holding the :term:`GIL` and ``0`` otherwise.
1160
1196
This function can be called from any thread at any time.
1161
- Only if it has had its Python thread state initialized and currently is
1162
- holding the :term:`GIL ` will it return ``1``.
1197
+ Only if it has had its :term:` thread state <attached thread state>` initialized
1198
+ via :c:func:`PyGILState_Ensure ` will it return ``1``.
1163
1199
This is mainly a helper/diagnostic function. It can be useful
1164
1200
for example in callback contexts or memory allocation functions when
1165
1201
knowing that the :term:`GIL` is locked can allow the caller to perform sensitive
1166
1202
actions or otherwise behave differently.
1167
1203
1204
+ .. note::
1205
+ If the current Python process has ever created a subinterpreter, this
1206
+ function will *always* return ``1``. Prefer :c:func:`PyThreadState_GetUnchecked`
1207
+ for most cases.
1208
+
1168
1209
.. versionadded:: 3.4
1169
1210
1170
1211
0 commit comments