I know how to interrupt the kernel (such as by tapping I
twice or by interrupting the kernel on the web interface). However, I built a C-extension for Python (I'm using Windows) that handles CTRL-C events in my C++ code (a toy example):
static int s_interrupted = 0;
BOOL WINAPI consoleHandler(DWORD fdwCtrlType) {
switch (fdwCtrlType)
{
// Handle the CTRL-C signal.
case CTRL_C_EVENT:
s_interrupted = 1;
return TRUE;
}
}
int main() {
s_interrupted = 0;
int output = 1;
if (!SetConsoleCtrlHandler(consoleHandler, TRUE)) {
std::cout<<"ERROR: Could not set control handler"<<std::endl;
} else {
std::cout<<"Control hanlder installed"<<std::endl;
}
int k = 10000;
while (int i < k) {
if (s_interrupted == 1) {
output = -1;
break;
}
output = i
i = i + 1;
}
return output;
}
The output of my main program changes depending on the value of s_interrupted
. In other words, if I don't press CTRL+C, the program will finish the while loop and return an integer. If I press CTRL+C, the while loop will be terminated and return a different integer. I don't expect to see a KeyboardInterrupt
in my Python terminal.
It works fine, when I call this C extension in the terminal. However, when I do it in a Jupyter notebook, the program behaves as if I never interrupted the kernel. Does Jupyter not send a SIGINT
when I interrupt the kernel?