php pcntl_fork() 返回,致命错误:调用未定义的函数 pcntl_fork()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16826530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pcntl_fork() returning, Fatal error: Call to undefined function pcntl_fork()
提问by Ben Pearce
I'm trying to fork a command line run XAMPP php process using pcntl_fork(). When I run the command below:
我正在尝试使用 pcntl_fork() fork 命令行运行 XAMPP php 进程。当我运行以下命令时:
$pid = pcntl_fork();
if($pid == -1){
file_put_contents('testlog.log',"\r\nFork Test",FILE_APPEND);
return 1; //error
}
else if($pid){
return 0; //success
}
else{
file_put_contents($log, 'Running...', FILE_APPEND);
}
I get:
我得到:
Fatal error: Call to undefined function pcntl_fork()
Can anyone suggest how to fix this?
谁能建议如何解决这个问题?
回答by Andrea
It is not possible to use the function 'pcntl_fork' when PHP is used as Apache module (such as XAMPP). You can only use pcntl_fork in CGI mode or from command-line.
当 PHP 用作 Apache 模块(例如 XAMPP)时,无法使用函数 'pcntl_fork'。您只能在 CGI 模式下或从命令行使用 pcntl_fork。
Using this function will result in: 'Fatal error: Call to undefined function: pcntl_fork()'
使用此函数将导致: 'Fatal error: Call to undefined function: pcntl_fork()'
回答by Stuart Carnie
To see if it is installed, run:
要查看它是否已安装,请运行:
php -i | grep pcntl
php -i | grep pcntl
If it is present and enabled then the pcntl function are likely disabled, which appears to be the default in newer PHP 5.x installs. To check, run:
如果它存在并启用,则 pcntl 功能可能被禁用,这似乎是较新的 PHP 5.x 安装中的默认设置。要检查,请运行:
php -i | grep disable_functions
php -i | grep disable_functions
If you see a list of pcntl_* functions, you'll need to edit your php.ini file (inside of XAMPP) and comment out the line disable_functions=
如果您看到 pcntl_* 函数列表,则需要编辑 php.ini 文件(在 XAMPP 内)并注释掉该行 disable_functions=
I'd recommend you use this distributionof PHP for OS X, which has current versions and I can confirm does have the pcntl
extension.
回答by Nabi K.A.Z.
pcntl_*
functions, Process Control support in PHP is not enabled by default. You have to compile the CGI or CLI version (don't used as Apache module) of PHP with --enable-pcntl
configuration option when compiling PHP to enable Process Control support.
pcntl_*
功能,PHP 中的进程控制支持默认不启用。--enable-pcntl
在编译 PHP 以启用进程控制支持时,您必须使用配置选项编译 PHP的 CGI 或 CLI 版本(不要用作 Apache 模块)。
Currently, this module will not function on non-Unix platforms(Windows).
目前,该模块在非 Unix 平台(Windows)上不起作用。
回答by JVE999
I had the same issue when running the script as a part of Apache. So, my solution was to put the code containing pcntl_fork
in a different file (let's call it fork.php
) and use exec()
to run it, like this:
作为 Apache 的一部分运行脚本时,我遇到了同样的问题。所以,我的解决方案是将包含pcntl_fork
在不同文件中的代码(让我们称之为fork.php
)并使用exec()
它来运行它,如下所示:
mainFile.php (this is what Apache will run)
mainFile.php(这是 Apache 将运行的)
exec('php fork.php',$output);
fork.php
叉子.php
$pid = pcntl_fork();
if($pid == -1){
file_put_contents('testlog.log',"\r\nFork Test",FILE_APPEND);
return 1; //error
}
else if($pid){
return 0; //success
}
else{
file_put_contents($log, 'Running...', FILE_APPEND);
}