3

I have a c program basically is running in infinite While loop as shown below.

int main(int argc, char** argv )
{    
     Detectdirection *d;
     //Mosquitto
     io_service io;
     deadline_timer t(io);
     MosquitoInterface *m = new MosquitoInterface(t);



     d = new Detectdirection();      
     while(true)
     {        
        int ret =  d->Tracking();
        if(ret < 0)
           cout << "Pattern is not found" << endl ;
     }  
     delete d;
     delete m;
     cout << "Exit" << endl;
     return 0;
}

I like to run and stop the program from python script. Running the program is quite straightforward. Just to provide a path to the build file as discussed here.

But how can I stop the application from Pyhton so that those objects created are deleted properly.

Community
  • 1
  • 1
batuman
  • 7,066
  • 26
  • 107
  • 229
  • You can't. If you kill the program the objects will not be destructed properly. The memory will be released, but their destructors will not be called. The only way to do that is to somehow end the loop and let the program exit the normal way. I suggest you look up how other *daemons* do it. – Some programmer dude Nov 29 '16 at 09:45
  • Is that the `io_service` from boost ASIO?, You could actually have an async task waiting for one of those messages I described in my answer. – Lanting Nov 29 '16 at 09:55

3 Answers3

6

You need to communicate between the processes.

Sending a signal to the process (and catching that signal in the executable) is a common method of doing this.

Sending the signal from python: How to terminate a python subprocess launched with shell=True

Handling the signal: Signal Handling in C

Alternatively you could use more advanced techniques for sending messages (for instance zeromq Understanding ZeroMQ , dbus https://stackoverflow.com/questions/tagged/dbus , or COM COM, COM+, DCOM, where to start? )

The keyword here is inter-process communication. It has its own wikipedia article https://en.wikipedia.org/wiki/Inter-process_communication

Community
  • 1
  • 1
Lanting
  • 3,060
  • 12
  • 28
0

You can excute shell scripe using function os.system(). So I support using code as below, which sends a kill signal to C process(name of C process is 'name':

def processinfo(x):
    p = psutil.get_process_list()
    for r in p:
        aa = str(r)
        f = re.compile(x,re.I)
        if f.search(aa):
            return aa.split('pid=')
os.system('kill -9 ' + processinfo(name))
flyingrose
  • 107
  • 2
  • 11
0

Start the program using python's subprocess.Popen rather than the convenience function call. The Popen object has a .terminate() method which sends a signal to the program which will stop the program in a controlled manner. How the program behaves on termination is up to the C program not python. The SIGTERM can be caught and handled by your program if it registers a callback using signal().

I suggest you write some test programs and if necessary read up on signal handling in C and on windows.

Mike Robins
  • 1,733
  • 10
  • 14