Checks if the Python interpreter supports free-threaded mode. Returns true if Python is free-threaded (GIL disabled), false otherwise.
()
| 3 | /// Checks if the Python interpreter supports free-threaded mode. |
| 4 | /// Returns true if Python is free-threaded (GIL disabled), false otherwise. |
| 5 | pub fn is_free_threaded_python() -> bool { |
| 6 | // Use sysconfig.get_config_var("Py_GIL_DISABLED") as recommended by Python docs at https://docs.python.org/3/howto/free-threading-python.html#identifying-free-threaded-python |
| 7 | let output = Command::new("python") |
| 8 | .args([ |
| 9 | "-c", |
| 10 | "import sysconfig; print(sysconfig.get_config_var('Py_GIL_DISABLED') or 0)", |
| 11 | ]) |
| 12 | .output(); |
| 13 | |
| 14 | match output { |
| 15 | Ok(output) if output.status.success() => { |
| 16 | let stdout = String::from_utf8_lossy(&output.stdout); |
| 17 | stdout.trim() == "1" |
| 18 | } |
| 19 | _ => false, // If Python is not available or command fails, assume not free-threaded |
| 20 | } |
| 21 | } |