Struct pyo3::sync::GILProtected
source · pub struct GILProtected<T> { /* private fields */ }Expand description
Value with concurrent access protected by the GIL.
This is a synchronization primitive based on Python’s global interpreter lock (GIL). It ensures that only one thread at a time can access the inner value via shared references. It can be combined with interior mutability to obtain mutable references.
Example
Combining GILProtected with RefCell enables mutable access to static data:
use pyo3::sync::GILProtected;
use std::cell::RefCell;
static NUMBERS: GILProtected<RefCell<Vec<i32>>> = GILProtected::new(RefCell::new(Vec::new()));
Python::with_gil(|py| {
NUMBERS.get(py).borrow_mut().push(42);
});