-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Add FAQ entry for dealing with long functions interruption #2000
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
Conversation
I've dabbled with interrupts so I appreciate this. What would be an example of |
Sorry I didn't include it, it's a user-defined exception since C++ stdexcept has no equivalent:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor wording request.
Thanks! |
Nice! From tinkering with this, I think this could possibly be simplified; will PR a small change. Reading the docs, it looks like the exception would have already been raised if this function returns non-zero: So rather than create a redundant m.def("stuff", []() -> void {
while (true) {
if (PyErr_CheckSignals() != 0) {
throw py::error_already_set();
}
using namespace std::chrono_literals;
std::this_thread::sleep_for(50ms);
}
}); try:
m.stuff()
assert False
except KeyboardInterrupt:
print("[ Done ]") Full code here: https://github.com/eacousineau/repro/blob/f791b817ae88743cff66f7214f40a487ae99d843/python/pybind11/custom_tests/test_tmp.cc |
Posted: #2007 |
Great, much simpler with |
I've spent some time figuring out how to correctly interrupt a long-running function when a SIGINT occurs, so I thought this entry could be useful.