pub fn with_critical_section<F, R>(object: &Bound<'_, PyAny>, f: F) -> Rwhere
F: FnOnce() -> R,
Expand description
Executes a closure with a Python critical section held on an object.
Acquires the per-object lock for the object op
that is held
until the closure f
is finished.
This is structurally equivalent to the use of the paired Py_BEGIN_CRITICAL_SECTION and Py_END_CRITICAL_SECTION C-API macros.
A no-op on GIL-enabled builds, where the critical section API is exposed as a no-op by the Python C API.
Provides weaker locking guarantees than traditional locks, but can in some cases be used to provide guarantees similar to the GIL without the risk of deadlocks associated with traditional locks.
Many CPython C API functions do not acquire the per-object lock on objects passed to Python. You should not expect critical sections applied to built-in types to prevent concurrent modification. This API is most useful for user-defined types with full control over how the internal state for the type is managed.