如何在Apache中使用pcntl_fork()?

时间:2012-08-31 11:50:51

标签: php apache pcntl

这是我的代码,在index.php内(仅举例):

$pid = pcntl_fork();
if ($pid == -1) {
  die("failed to fork");
} else if ($pid) {
  // nothing to do
} else {
  putDataIntoWebService();
  exit();
}
echo "normal page content";

此代码段在命令行中运行正常。在Apache exit()中杀死他们,父和子进程。什么是变通方法?

2 个答案:

答案 0 :(得分:5)

您不能将pcntl_*函数与PHP的Apache模块版本一起使用。引用pcntl_fork documentation中的评论:

  

使用PHP时,无法使用“pcntl_fork”功能   作为Apache模块。您只能在CGI模式或从中使用pcntl_fork   命令行。

     

使用此功能将导致:'致命错误:调用未定义   功能:pcntl_fork()'

答案 1 :(得分:1)

这是解决方案:

posix_kill(getmypid(), SIGKILL);

而不是exit()