signal_safe_block_on

Function signal_safe_block_on 

Source
pub fn signal_safe_block_on<F>(py: Python<'_>, future: F) -> PyResult<F::Output>
where F: Future + Send + 'static, F::Output: Send + 'static,
Expand description

Block the current thread on a future, but make sure to check for signals originating from the Python signal handler.

Python’s signal handler just sets a flag that it expects the Python interpreter to handle later via a call to PyErr_CheckSignals. When we enter into potentially long-running native code, we need to make sure to be checking for signals frequently, otherwise we will ignore them. This will manifest as ctrl-C not doing anything.

One additional wrinkle is that PyErr_CheckSignals only works on the main Python thread; if it’s called on any other thread it silently does nothing. So, we check if we’re on the main thread by comparing native thread IDs.