开发者

Best way to start, stop and send parameters to separate Python script from C++ application?

I try to explain the situation:

  • I have a QT application written in C++ and QT.

  • This QT application starts a separate console C++ application that runs in the background.

  • These two communicate using perhaps sockets, don't know yet.

  • Console C++ application needs to start and stop my gnuradio python script. Also it needs to send parameters to it.

  • Once started, this gnuradio script runs independedly in infinite loop sending information to either the console or the QT application using sockets perhaps.

  • My console application needs to stop this gnuradio script from running when the order is given by the QT application.

The question is how can I stop this separate python script from my C++ console application ? Also is there anything I could do to m开发者_开发百科ake this more simple ?

Regards,

Spitz


Sockets, or you could use DBUS python, and DBUS c++, if you want to be all free-desktopy :D


Spawn python script as a new process using fork() and execv(). execv() (or any other function of the exec family) lets you pass arguments to the Python script. Use the child process ID to send a kill signal when you are done with the Python script.


For your C++ program, you may wanna take a look here :

http://www.codeproject.com/KB/cpp/kill_process.aspx

Its gives you the basic code for creating and killing an external process. Remember that launching a python script means calling the python bin and giving the script as first argument.

The communication between your C++ app and the python script can be made via a named pipe

http://en.wikipedia.org/wiki/Named_pipe

but DBUS can work too.

My advice is :

1) start your C++ app from your QT app using QT's goodness. You can have these two communicate via standard I/O redirection (depending or what you really wanan do)

2) start your python script from your C++ following the example given above. And those two communicate via DBUS/Socket/Pipes.

Should do the trick

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜