mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-24 14:15:11 +00:00
Minor modifications to interrupt handling FAQ (#2007)
This commit is contained in:
parent
0f1d3bfee2
commit
baf69345f6
16
docs/faq.rst
16
docs/faq.rst
@ -257,30 +257,22 @@ is released, so a long-running function won't be interrupted.
|
|||||||
To interrupt from inside your function, you can use the ``PyErr_CheckSignals()``
|
To interrupt from inside your function, you can use the ``PyErr_CheckSignals()``
|
||||||
function, that will tell if a signal has been raised on the Python side. This
|
function, that will tell if a signal has been raised on the Python side. This
|
||||||
function merely checks a flag, so its impact is negligible. When a signal has
|
function merely checks a flag, so its impact is negligible. When a signal has
|
||||||
been received, you can explicitely interrupt execution by throwing an exception
|
been received, you must either explicitly interrupt execution by throwing
|
||||||
that gets translated to KeyboardInterrupt (see :doc:`advanced/exceptions`
|
``py::error_already_set`` (which will propagate the existing
|
||||||
section):
|
``KeyboardInterrupt``), or clear the error (which you usually will not want):
|
||||||
|
|
||||||
.. code-block:: cpp
|
.. code-block:: cpp
|
||||||
|
|
||||||
class interruption_error: public std::exception {
|
|
||||||
public:
|
|
||||||
const char* what() const noexcept {
|
|
||||||
return "Interruption signal caught.";
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
PYBIND11_MODULE(example, m)
|
PYBIND11_MODULE(example, m)
|
||||||
{
|
{
|
||||||
m.def("long running_func", []()
|
m.def("long running_func", []()
|
||||||
{
|
{
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (PyErr_CheckSignals() != 0)
|
if (PyErr_CheckSignals() != 0)
|
||||||
throw interruption_error();
|
throw py::error_already_set();
|
||||||
// Long running iteration
|
// Long running iteration
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
py::register_exception<interruption_error>(m, "KeyboardInterrupt");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Inconsistent detection of Python version in CMake and pybind11
|
Inconsistent detection of Python version in CMake and pybind11
|
||||||
|
Loading…
Reference in New Issue
Block a user