Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
pybind11
Commits
baf69345
Commit
baf69345
authored
Nov 25, 2019
by
Eric Cousineau
Committed by
Wenzel Jakob
Nov 25, 2019
Browse files
Minor modifications to interrupt handling FAQ (#2007)
parent
0f1d3bfe
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
4 additions
and
12 deletions
+4
-12
docs/faq.rst
docs/faq.rst
+4
-12
No files found.
docs/faq.rst
View file @
baf69345
...
@@ -257,30 +257,22 @@ is released, so a long-running function won't be interrupted.
...
@@ -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
explicit
e
ly 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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment