
Am 09.11.2016 um 23:26 schrieb Gavin Lambert:
On 10/11/2016 09:00, Klemens Morgenstern wrote:
That did however allow one additional feature: with the pipe automatically closing, you can now use a part of the asio stuff with bp::spawn.
asio::io_service ios; std::future<std::string> fut; bp::spawn("foo", std_out > fut, ios);
Not sure this is the smartest thing to do, but it could make sense for some simple applications.
That's quite nice, actually -- a large number of applications would just want to pull the complete output from a process, either with no input or with completely provided input. It's probably less common to need to hold an interactive conversation, so having a nice quick syntax that hides the stream/pipe work internally is a pretty good win.
Well the problem is the potential deadlock, since you cannot terminate the process if it hangs. But that's a risk conciously taken if you don't store the handle. Makes sense for some processes though.
Having said that, is there an inverse of this for providing the complete input? Presumably std_in < std::string(x) can't work, as this would be confused with a filename. (And while wrapping it in a future is possible, it'd be a bit silly.) It's "std_in < asio::buffer(x)", whereby buffer is also pulled into the process namespace. And you can also use a future which tells when the write is complete:
std::string x; std::future<void> f; bp::spawn("foo", std_in < asio::buffer(x) > f); and yeah, that looks a bit weird, but I couldn't think of a better operator for this.
This sort of thing kinda makes me wish that the originally proposed future_stream survived into Boost or the standard. Though you can simulate one with a composite future (a future that returns a value and a new future); I have an implementation kicking around somewhere. It's not the prettiest of things nor would it be a simple drop-in though, I think.
If there was some facility like that to use with boost.asio (which has a streambuf implementation) you could use it. I could add lazy syntax for a popular use-case. That one currently works asio::streambuf buf; bp::spawn("foo", std_out > buf); std::istream(&buf); //but here I don't know how thread-safe that is, that would be a question for boost.asio.