Merge pull request #3560 from Jgfrausing/patch-1

docs: Include section on how to disable signals in python
This commit is contained in:
David Hewitt 2024-02-06 09:01:17 +00:00 committed by GitHub
commit 059e485a95
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 21 additions and 0 deletions

View File

@ -487,5 +487,26 @@ class House(object):
}
```
## Handling system signals/interrupts (Ctrl-C)
The best way to handle system signals when running Rust code is to periodically call `Python::check_signals` to handle any signals captured by Python's signal handler. See also [the FAQ entry](./faq.md#ctrl-c-doesnt-do-anything-while-my-rust-code-is-executing).
Alternatively, set Python's `signal` module to take the default action for a signal:
```rust
use pyo3::prelude::*;
# fn main() -> PyResult<()> {
Python::with_gil(|py| -> PyResult<()> {
let signal = py.import("signal")?;
// Set SIGINT to have the default action
signal
.getattr("signal")?
.call1((signal.getattr("SIGINT")?, signal.getattr("SIG_DFL")?))?;
Ok(())
})
# }
```
[`PyModule::new`]: {{#PYO3_DOCS_URL}}/pyo3/types/struct.PyModule.html#method.new