how to use pcntl_fork() with Apache?

喜欢而已 提交于 2019-12-02 03:34:07

问题


This is my code, inside index.php (just an example):

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

This snippet works fine in command line. In Apache exit() kills them both, the parent and the kid process. What is a workaround?


回答1:


You can't use the pcntl_* functions with the Apache module version of PHP. Quoting from a comment in the pcntl_fork documentation:

It is not possible to use the function 'pcntl_fork' when PHP is used as Apache module. You can only use pcntl_fork in CGI mode or from command-line.

Using this function will result in: 'Fatal error: Call to undefined function: pcntl_fork()'




回答2:


This is the solution:

posix_kill(getmypid(), SIGKILL);

instead of exit().



来源:https://stackoverflow.com/questions/12214785/how-to-use-pcntl-fork-with-apache

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