为什么Python check_output()在调用守护进程时没有返回?

时间:2016-02-05 01:02:02

标签: python c++ fork daemon

我有一个Python v3.4应用程序,它使用check_output()来调用调用fork()的C ++应用程序,原始进程退出并继续子进程。似乎check_output()也在等待子进程,而不是在主进程返回后返回,并说守护进程已成功启动。

我是否需要更改C ++中fork()的方式,或者需要以某种方式告知Python check_output()调用仅等待父进程退出?我需要在C ++中用described here做第二个fork()吗?

这是剥离了Python的问题:

#! /usr/local/bin/python3

import logging
import argparse
from subprocess import CalledProcessError, check_output, STDOUT

ARGS = ["user@hostname:23021:"]

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="Try launching subprocess")
    parser.add_argument("exec", type=str, help="the exec to run")
    args = parser.parse_args()

    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s - %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')

    cmd_list = [args.exec] + ARGS
    logging.info(str(cmd_list))

try:
    output = check_output(cmd_list,
                          stderr=STDOUT,
                          universal_newlines=True)
    logging.info("Exec OK with output:")
    logging.info(output)
except CalledProcessError as e:
    logging.info("Exec Not OK with output:")
    logging.info(str(e))

这是用于守护C ++应用程序的C ++代码:

void
daemonize()
{
  // This routine backgrounds the process to run as a daemon.
  // Returns to caller only if we are the child, otherwise exits normally.
  if (getppid() == 1) {
    return;  // Leave if we're already a daemon
  }

  // Create the backgrounded child process.
  const pid_t parent = getpid();
  const pid_t pid = fork();
  CSysParamAccess param;
  const string progName(param.getProgramKindName());

  ::close(STDIN_FILENO);
  ::close(STDOUT_FILENO);
  ::close(STDERR_FILENO);

  if (pid < 0) {
    cerr << "Error: " << progName << " failed to fork server. Aborting."
         << endl; // inform the client of the failure

    exit(appExit::failForkChild);    // Error. No child created.
  } else if (pid > 0)  {
    // We're in the parent.  Optionally print the child's pid, then exit.
    if (param.getDebug()) {
      clog << "Successful fork. The Application server's (" << progName
           << ") pid is: " << pid << "(self) from parent " << parent << endl;
    }

    ::close(STDIN_FILENO);
    ::close(STDOUT_FILENO);
    ::close(STDERR_FILENO);

    exit(appExit::normal);
  }

  ::close(STDIN_FILENO);
  ::close(STDOUT_FILENO);
  ::close(STDERR_FILENO);

  // Here only in the child (daemon).
  if (-1 == setsid()) { // Get a new process group
    cerr << "Error: Failed to become session leader while daemonising - errno: "
         << errno;

    exit(appExit::failForkChild);    // Error.  Child failed.
  }

  signal(SIGHUP, SIG_IGN); // Per example.

  // Fork again, allowing the parent process to terminate.
  const pid_t midParent = getpid();
  const pid_t grandChildPid = fork();

  if (grandChildPid < 0) {
    cerr << "Error: Failed to fork while daemonising - errno: " << errno;

    exit(appExit::failForkChild);    // Error.  GrandChild failed.
  } else if (grandChildPid > 0) {
    // We're in the parent.  Optionally print the grandchild's pid, then exit.
    if (param.getDebug()) {
      clog << "Successful second fork. The Application server's (" << progName
           << ") pid is: " << grandChildPid << "(self) from parent "
           << midParent << endl;
    }

    ::close(STDIN_FILENO);
    ::close(STDOUT_FILENO);
    ::close(STDERR_FILENO);

    exit(appExit::normal);
  }

  // Here only in the grandchild (daemon).
  appGlobalSetSignalHandlers();

  // Set the current working directory to the root directory.
  if (chdir("/") == -1) {
    cerr <<
      "Error: Failed to change working directory while daemonising - errno:"
         << errno;

    exit(appExit::failForkChild);    // Error.  GrandChild failed.
  }

  // Set the user file creation mask to zero.
  umask(0);

  //close(STDIN_FILENO); // Cannot close due to assertion in transfer.cpp
  // Theoretically, we would reopen stderr and stdout using the log file.
  ::close(STDIN_FILENO);
  ::close(STDOUT_FILENO);
  ::close(STDERR_FILENO);

  // We only return here if we're the grandchild process, the Application
  // server.  The summoner exited in daemonize().
  clog << "Application " << param.getProgramKindName()
       << " (" << appGlobalProgramName() << ") successfully started." << endl;
}

在使用echo调用时可以正常工作,并且在我的C ++应用程序中失败:

> stuckfork.py echo
2016-02-05 10:17:34 - ['echo', 'user@hostname:23021:']
2016-02-05 10:17:34 - Exec OK with output:
2016-02-05 10:17:34 - user@hostname:23021:

> stuckfork.py flumep
2016-02-05 10:17:53 - ['flumep', 'user@hostname:23021:']
  C-c Traceback (most recent call last):
  File "/home/user/Bin/Bin/stuckfork.py", line 26, in <module>
    universal_newlines=True)
  File "/usr/local/lib/python3.4/subprocess.py", line 609, in check_output
    output, unused_err = process.communicate(inputdata, timeout=timeout)
  File "/usr/local/lib/python3.4/subprocess.py", line 947, in communicate
    stdout = _eintr_retry_call(self.stdout.read)
  File "/usr/local/lib/python3.4/subprocess.py", line 491, in _eintr_retry_call
    return func(*args)
KeyboardInterrupt
>

我已经将问题缩小到我的一个C ++静态构造函数正在做的事情导致启动过程失效,这就是Python仍在等待的原因。现在划分看哪一个。

2 个答案:

答案 0 :(得分:1)

一个正确的解决方案是找到正确的文件描述符,它将输出管道从分叉的C ++子进程传递给python并关闭它。

现在,您可以在C ++子进程中或在调用子进程之前(在fork()之后)尝试close(1) SYSTEM CALL。这将告诉python停止尝试从孩子那里读书。

我不确定这是否有效,因为您发布的代码还不够。

答案 1 :(得分:0)

问题是打开文件描述符,这是因为运行了这个静态代码:

re.findall(r'\([^()]* W/O [^)]*\)', foo)

删除该行后,守护程序FILE *origStdErr = fdopen(dup(fileno(stderr)), "a"); close(0)close(1)的效果正常,Python代码停止等待。