C API Extension Support for Free Threading

Starting with the 3.13 release, CPython has experimental support for running with the 全局解释器锁 (GIL) disabled in a configuration called free threading . This document describes how to adapt C API extensions to support free threading.

Identifying the Free-Threaded Build in C

The CPython C API exposes the Py_GIL_DISABLED macro: in the free-threaded build it’s defined to 1 , and in the regular build it’s not defined. You can use it to enable code that only runs under the free-threaded build:

#ifdef Py_GIL_DISABLED
/* code that only runs in the free-threaded build */
#endif