gpt4 book ai didi

c++ - 使用 POCO 启动后台进程

转载 作者:行者123 更新时间:2023-11-30 05:27:17 32 4
gpt4 key购买 nike

我正在使用 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 vector 上尝试了“&”,但它没有用!

谢谢,

最佳答案

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

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;
}

希望这对其他人有帮助!

关于c++ - 使用 POCO 启动后台进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37460127/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com