mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-25 09:02:00 +00:00
ShellCommand: automatic wait call in destructor (#1210)
* ShellCommand: automatic wait call in destructor * ShellCommand: tryWait in destructor * Update ShellCommand.cpp * Update ShellCommand.cpp
This commit is contained in:
parent
6911974d56
commit
d422777d9c
@ -76,6 +76,11 @@ namespace
|
||||
namespace DB
|
||||
{
|
||||
|
||||
ShellCommand::~ShellCommand()
|
||||
{
|
||||
if (!wait_called)
|
||||
tryWait();
|
||||
}
|
||||
|
||||
std::unique_ptr<ShellCommand> ShellCommand::executeImpl(const char * filename, char * const argv[], bool pipe_stdin_only)
|
||||
{
|
||||
@ -176,6 +181,8 @@ std::unique_ptr<ShellCommand> ShellCommand::executeDirect(const std::string & pa
|
||||
|
||||
int ShellCommand::tryWait()
|
||||
{
|
||||
wait_called = true;
|
||||
|
||||
int status = 0;
|
||||
if (-1 == waitpid(pid, &status, 0))
|
||||
throwFromErrno("Cannot waitpid", ErrorCodes::CANNOT_WAITPID);
|
||||
|
@ -27,6 +27,7 @@ class ShellCommand
|
||||
{
|
||||
private:
|
||||
pid_t pid;
|
||||
bool wait_called = false;
|
||||
|
||||
ShellCommand(pid_t pid, int in_fd, int out_fd, int err_fd)
|
||||
: pid(pid), in(in_fd), out(out_fd), err(err_fd) {};
|
||||
@ -38,6 +39,8 @@ public:
|
||||
ReadBufferFromFile out;
|
||||
ReadBufferFromFile err;
|
||||
|
||||
~ShellCommand();
|
||||
|
||||
/// Run the command using /bin/sh -c
|
||||
static std::unique_ptr<ShellCommand> execute(const std::string & command, bool pipe_stdin_only = false);
|
||||
|
||||
|
@ -5,6 +5,8 @@
|
||||
#include <IO/WriteBufferFromFileDescriptor.h>
|
||||
#include <IO/ReadBufferFromString.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
using namespace DB;
|
||||
|
||||
@ -43,6 +45,15 @@ try
|
||||
|
||||
command->wait();
|
||||
}
|
||||
|
||||
// <defunct> hunting:
|
||||
for (int i = 0; i < 1000; ++i) {
|
||||
auto command = ShellCommand::execute("echo " + std::to_string(i));
|
||||
//command->wait(); // now automatic
|
||||
}
|
||||
|
||||
// std::cerr << "inspect me: ps auxwwf" << "\n";
|
||||
// std::this_thread::sleep_for(std::chrono::seconds(100));
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user