Function pyo3::prepare_freethreaded_python[][src]

pub fn prepare_freethreaded_python()

Prepares the use of Python in a free-threaded context.

If the Python interpreter is not already initialized, this function will initialize it with disabled signal handling (Python will not raise the KeyboardInterrupt exception). Python signal handling depends on the notion of a 'main thread', which must be the thread that initializes the Python interpreter.

If both the Python interpreter and Python threading are already initialized, this function has no effect.

Availability

This function is only available when linking against Python distributions that contain a shared library.

This function is not available on PyPy.

Panics

  • If the Python interpreter is initialized but Python threading is not, a panic occurs. It is not possible to safely access the Python runtime unless the main thread (the thread which originally initialized Python) also initializes threading.

Example

use pyo3::prelude::*;

fn main() {
    pyo3::prepare_freethreaded_python();
    Python::with_gil(|py| {
        py.run("print('Hello World')", None, None)
    });
}