pub struct GILProtected<T> { /* private fields */ }
👎Deprecated since 0.26.0: Prefer an interior mutability primitive compatible with free-threaded Python, such as
Mutex
in combination with the MutexExt
traitExpand 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.
This type is not defined for extensions built against the free-threaded CPython ABI.
§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::attach(|py| {
NUMBERS.get(py).borrow_mut().push(42);
});
Implementations§
Trait Implementations§
impl<T> Sync for GILProtected<T>where
T: Send,
Available on non-
Py_GIL_DISABLED
only.Auto Trait Implementations§
impl<T> Freeze for GILProtected<T>where
T: Freeze,
impl<T> RefUnwindSafe for GILProtected<T>where
T: RefUnwindSafe,
impl<T> Send for GILProtected<T>where
T: Send,
impl<T> Unpin for GILProtected<T>where
T: Unpin,
impl<T> UnwindSafe for GILProtected<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more