Boost::process async_wait process

断了今生、忘了曾经 提交于 2021-02-11 10:50:37

问题


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

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?

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


回答1:


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



回答2:


Boost.Process is not an official Boost library. I can find no reference to the work in progress on this library on the author's blog later than this which is two years old.

I am not sure where is the "right place" to get the code given this uncertain prognosis. You could ask on the Boost forums - maybe there is a current/working test version in their repository that you can pick up.



来源:https://stackoverflow.com/questions/3827734/boostprocess-async-wait-process

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!