Skip to content

Minor modifications to interrupt handling FAQ #2007

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 25, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions docs/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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()``
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
been received, you can explicitely interrupt execution by throwing an exception
that gets translated to KeyboardInterrupt (see :doc:`advanced/exceptions`
section):
been received, you must either explicitly interrupt execution by throwing
``py::error_already_set`` (which will propagate the existing
``KeyboardInterrupt``), or clear the error (which you usually will not want):

.. code-block:: cpp

class interruption_error: public std::exception {
public:
const char* what() const noexcept {
return "Interruption signal caught.";
}
};

PYBIND11_MODULE(example, m)
{
m.def("long running_func", []()
{
for (;;) {
if (PyErr_CheckSignals() != 0)
throw interruption_error();
throw py::error_already_set();
// Long running iteration
}
});
py::register_exception<interruption_error>(m, "KeyboardInterrupt");
}

Inconsistent detection of Python version in CMake and pybind11
Expand Down