Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't kill process by Ctrl-c in a boost.python module with an endless loop

I have make a boost.python module with an endless loop.But I can't kill the process by ctrl-c.The following is an example.

C++

#include <boost/python.hpp>
#include <boost/python.module.hpp>
#include <boost/python.def.hpp>
#include <iostream>
usring namespace boost::python;

void foo() {
   int it=0;
   while (true) {                     //endless loop
       ++it;
       std::cout<< it <<std::endl;
       sleep(3);
   }
}

BOOST_PYTHON_MODULE(ctopy)
{
    def("foo",foo);
}

python:

import ctopy
ctopy.foo()

result:

1
2
3
4
.....................

I can't kill the foreground process by Ctrl-c.why the module don't accept signal "SIGINT" that was sent by Ctrl-c.How to make it work.

like image 415
simon Avatar asked Oct 15 '25 14:10

simon


1 Answers

you should call PyErr_CheckSignals() periodically in your extension code and call exit() if it returns -1:

while (true) { //endless loop
    ++it;
    std::cout<< it <<std::endl;
    sleep(3);
    if(PyErr_CheckSignals() == -1) {
        exit(1);
    }
  }
like image 146
iabdalkader Avatar answered Oct 18 '25 04:10

iabdalkader



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!