Boost :: process async_wait进程 [英] Boost::process async_wait process

查看:67
本文介绍了Boost :: process async_wait进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建程序,并且正在尽最大可能异步进行.

I am creating a program and I'm doing the most possible asynchronously.

我需要运行一个程序,当该程序完成时,它将调用回调函数.我找到了boost :: process的版本并决定使用,但似乎有示例,但是在我下载的源代码中找不到实现,有人可以帮我吗?

I need to run a program and when this program finishes it calls a callback function. I found a version of boost::process and decided to use, but it seems that there is the example but could not find the implementation in the source that I downloaded, could someone give me a hand?

代码示例 http://www.highscore.de/boost/gsoc2010/process/user_guide.html#boost_process.user_guide.waiting 并在此处下载源boost :: process www.highscore.de/cpp/process/

code example http://www.highscore.de/boost/gsoc2010/process/user_guide.html#boost_process.user_guide.waiting and download source boost::process here www.highscore.de/cpp/process/

我需要为其创建一个实现,但是我从错误的地方获得了源代码?

I need to create an implementation for it or there but I got the sources from the wrong place?

这是解决我的问题的示例代码.

this is a sample code to resolve my problem.

boost::asio::io_service ioservice;

void end_wait(const boost::system::error_code &ec, int exit_code); 

int main() 
{ 
    std::string exe = boost::process::find_executable_in_path("hostname"); 
    std::vector<std::string> args; 
    boost::process::child c = boost::process::create_child(exe, args); 
    boost::process::status s(ioservice); 
    s.async_wait(c.get_id(), end_wait); 
    ioservice.run(); 
} 

void end_wait(const boost::system::error_code &ec, int exit_code) 
{ 
    if (!ec) 
    { 
#if defined(BOOST_POSIX_API) 
        if (WIFEXITED(exit_code)) 
            exit_code = WEXITSTATUS(exit_code); 
#endif 
        std::cout << "exit code: " << exit_code << std::endl; 
    } 
} 

对不起,我的英语不好问候布鲁诺

Sorry my bad english Regards Bruno

推荐答案

我知道这篇文章已经很老了,但是在Google中排名很高(我也遇到了同样的问题).该文档是错误的-整个 boost :: process :: status 类都不存在,并且 get_id()方法实际上是 id().

I know this post is quite old already, but it ranks very high in Google (I had the same problem). The documentation is wrong - The entire boost::process::status class doesn't exist, and the get_id() method is actually id().

这是异步等待进程退出的正确方法

Here's the correct way to asynchronously wait for a process to exit

#include <iostream>

#include <boost/asio.hpp>
#include <boost/process.hpp>
#include <boost/process/async.hpp>
#include <boost/process/extend.hpp>

struct MyHandler : boost::process::extend::async_handler {
    template <typename ExecutorType>
    std::function<void(int, std::error_code const&)> on_exit_handler(ExecutorType&) {
        return [](int exit_code, std::error_code const& ec) {
            if (ec) {
                // handle error
            }

#if defined(BOOST_POSIX_API)
            if (WIFEXITED(exit_code))
                exit_code = WEXITSTATUS(exit_code);
#endif
            std::cout << "exit code: " << exit_code << std::endl;
        };
    }
};

int main()
{
    boost::asio::io_service io;
    boost::process::child child{"/bin/echo", std::vector<std::string>{"testing"}, MyHandler{}, io};
    io.run();
}

这篇关于Boost :: process async_wait进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆