示例工程 21-free-threading-concurrency-choice
21-free-threading-concurrency-choice/01-detect-gil/detect_gil.py
"""打印当前解释器是否启用 GIL,以及构建配置。"""
from __future__ import annotations
import sys
import sysconfig
def gil_enabled() -> bool:
probe = getattr(sys, "_is_gil_enabled", None)
if probe is None:
return True
return bool(probe())
def free_threaded_build() -> bool:
return bool(sysconfig.get_config_var("Py_GIL_DISABLED"))
def main() -> None:
print(f"python: {sys.version.split()[0]}")
print(f"gil_enabled: {gil_enabled()}")
print(f"Py_GIL_DISABLED: {sysconfig.get_config_var('Py_GIL_DISABLED')}")
print(f"free_threaded_build: {free_threaded_build()}")
if __name__ == "__main__":
main()