使用 POCO 启动后台进程

launch background process with POCO

我正在使用 ROS 并尝试在 ROS QT 界面中集成 Poco Process 以进行流程管理。

这是我的节点到目前为止的示例:

void My_Interface::gazebo_launch_world()
{
    std::string command = "roslaunch";
    std::vector<std::string> args;
    args.push_back("robot_gazebo");
    args.push_back("robot_gazebo.launch");
    PocoProcess pp(command, args);
    pp.run();
}

int PocoProcess::run()
{
    int rc;
    try
    {
        Poco::Pipe outPipe;
        Poco::ProcessHandle ph = Poco::Process::launch(command_, args_, 0, &outPipe, 0);
        rc = ph.id();
        Poco::PipeInputStream istr(outPipe);
        Poco::StreamCopier::copyStream(istr, std::cout);
    }
    catch (Poco::SystemException& exc)
    {
        std::cout << exc.displayText() << std::endl;
        return (-1);
    }
    return rc;
}

这工作正常(启动进程)但是,问题是我的界面在等待进程完成时冻结,这确实是不可取的。

那么,我是否可以在后台启动 POCO 进程?

PS:(绝望地)我什至在 args 向量上尝试了“&”,但它没有用!

谢谢,

好的,解决方案是将管道与输出分离,因为它会阻塞等待显示它,我更改了以下代码(只是注释管道)并且它起作用了。

void My_Interface::gazebo_launch_world()
{
    std::string command = "roslaunch";
    std::vector<std::string> args;
    args.push_back("robot_gazebo");
    args.push_back("robot_gazebo.launch");
    PocoProcess pp(command, args);
    pp.run();
}

int PocoProcess::run()
{
    int rc;
    try
    {
        Poco::ProcessHandle ph = Poco::Process::launch(command_, args_);
        rc = ph.id();
    }
    catch (Poco::SystemException& exc)
    {
        std::cout << exc.displayText() << std::endl;
        return (-1);
    }
    return rc;
}

希望这对其他人有帮助!