@@ -1083,8 +1083,36 @@ Note that the ``PyGILState_*`` functions assume there is only one global
1083
1083
interpreter (created automatically by :c:func:`Py_Initialize`). Python
1084
1084
supports the creation of additional interpreters (using
1085
1085
:c:func:`Py_NewInterpreter`), but mixing multiple interpreters and the
1086
- ``PyGILState_*`` API is unsupported.
1086
+ ``PyGILState_*`` API is unsupported. This is because :c:func:`PyGILState_Ensure`
1087
+ and similar functions default to :term:`attaching <attached thread state>` a
1088
+ :term:`thread state` for the main interpreter, meaning that the thread can't safely
1089
+ interact with the calling subinterpreter.
1090
+
1091
+ Supporting subinterpreters in non-Python threads
1092
+ ------------------------------------------------
1093
+
1094
+ If you would like to support subinterpreters with non-Python created threads, you
1095
+ must use the ``PyThreadState_*`` API instead of the traditional ``PyGILState_*``
1096
+ API.
1097
+
1098
+ In particular, you must store the interpreter state from the calling
1099
+ function and pass it to :c:func:`PyThreadState_New`, which will ensure that
1100
+ the :term:`thread state` is targeting the correct interpreter::
1101
+
1102
+ /* The return value of PyInterpreterState_Get() from the
1103
+ function that created this thread. */
1104
+ PyInterpreterState *interp = ThreadData->interp;
1105
+ PyThreadState *tstate = PyThreadState_New(interp);
1106
+ PyThreadState_Swap(tstate);
1107
+
1108
+ /* GIL of the subinterpreter is now held.
1109
+ Perform Python actions here. */
1110
+ result = CallSomeFunction();
1111
+ /* evaluate result or handle exception */
1087
1112
1113
+ /* Destroy the thread state. No Python API allowed beyond this point. */
1114
+ PyThreadState_Clear(tstate);
1115
+ PyThreadState_DeleteCurrent();
1088
1116
1089
1117
.. _fork-and-threads:
1090
1118
@@ -1261,6 +1289,10 @@ code, or when embedding the Python interpreter:
1261
1289
.. seealso:
1262
1290
:c:func:`PyEval_ReleaseThread`
1263
1291
1292
+ .. note::
1293
+ Similar to :c:func:`PyGILState_Ensure`, this function will hang the
1294
+ thread if the runtime is finalizing.
1295
+
1264
1296
1265
1297
The following functions use thread-local storage, and are not compatible
1266
1298
with sub-interpreters:
@@ -1287,10 +1319,10 @@ with sub-interpreters:
1287
1319
When the function returns, there will be an :term:`attached thread state`
1288
1320
and the thread will be able to call arbitrary Python code. Failure is a fatal error.
1289
1321
1290
- .. note ::
1291
- Calling this function from a thread when the runtime is finalizing will
1292
- hang the thread until the program exits, even if the thread was not
1293
- created by Python. Refer to
1322
+ .. warning ::
1323
+ Calling this function when the runtime is finalizing is unsafe. Doing
1324
+ so will either hang the thread until the program ends, or fully crash
1325
+ the interpreter in rare cases. Refer to
1294
1326
:ref:`cautions-regarding-runtime-finalization` for more details.
1295
1327
1296
1328
.. versionchanged:: 3.14
@@ -1307,28 +1339,37 @@ with sub-interpreters:
1307
1339
Every call to :c:func:`PyGILState_Ensure` must be matched by a call to
1308
1340
:c:func:`PyGILState_Release` on the same thread.
1309
1341
1310
-
1311
1342
.. c:function:: PyThreadState* PyGILState_GetThisThreadState()
1312
1343
1313
1344
Get the :term:`attached thread state` for this thread. May return ``NULL`` if no
1314
1345
GILState API has been used on the current thread. Note that the main thread
1315
1346
always has such a thread-state, even if no auto-thread-state call has been
1316
1347
made on the main thread. This is mainly a helper/diagnostic function.
1317
1348
1318
- .. seealso: :c:func:`PyThreadState_Get``
1349
+ .. note::
1350
+ This function does not account for :term:`thread states <thread state>` created
1351
+ by something other than :c:func:`PyGILState_Ensure` (such as :c:func:`PyThreadState_New`).
1352
+ Prefer :c:func:`PyThreadState_Get` or :c:func:`PyThreadState_GetUnchecked`
1353
+ for most cases.
1319
1354
1355
+ .. seealso: :c:func:`PyThreadState_Get``
1320
1356
1321
1357
.. c:function:: int PyGILState_Check()
1322
1358
1323
1359
Return ``1`` if the current thread is holding the :term:`GIL` and ``0`` otherwise.
1324
1360
This function can be called from any thread at any time.
1325
- Only if it has had its Python thread state initialized and currently is
1326
- holding the :term:`GIL ` will it return ``1``.
1361
+ Only if it has had its :term:` thread state <attached thread state>` initialized
1362
+ via :c:func:`PyGILState_Ensure ` will it return ``1``.
1327
1363
This is mainly a helper/diagnostic function. It can be useful
1328
1364
for example in callback contexts or memory allocation functions when
1329
1365
knowing that the :term:`GIL` is locked can allow the caller to perform sensitive
1330
1366
actions or otherwise behave differently.
1331
1367
1368
+ .. note::
1369
+ If the current Python process has ever created a subinterpreter, this
1370
+ function will *always* return ``1``. Prefer :c:func:`PyThreadState_GetUnchecked`
1371
+ for most cases.
1372
+
1332
1373
.. versionadded:: 3.4
1333
1374
1334
1375
0 commit comments