Kill C++ process in python using ctrl + C

I have a python wrapper for a c++ program using Python.h In C++, the code contains an infinite loop as service:

for (;;) {

The python wrapper invokes this service and jumps into this loop. When I call ctrl+c, the program can't be killed. (It seems that the python program has been killed and the c++ program not)

How can I kill both of the python and c++ program using ctrl + c?

1 answer

  • answered 2018-05-16 05:54 MKP

    If you have pid of the child process, you can kill it using os.kill(pid, sig). To launch it when your python exits, you may add .atexit handler.