【问题标题】:pcntl_fork() returning, Fatal error: Call to undefined function pcntl_fork()pcntl_fork() 返回,致命错误:调用未定义函数 pcntl_fork()
【发布时间】:2013-05-25 11:01:51
【问题描述】:

我正在尝试使用 pcntl_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);
}

我明白了:

Fatal error: Call to undefined function pcntl_fork()

谁能建议如何解决这个问题?

【问题讨论】:

  • 你的操作系统是什么?请注意,Windows 没有底层的 *fork() 系统调用。
  • php5-pcntl安装成功了吗?
  • 不,我没有意识到这是需要与 XAMPP 分开安装的东西。
  • 是的。如果您通过 MacPorts 安装 PHP,请尝试 port install php5-pcntl
  • 抱歉,我错过了 XAMPP 部分。我的错。

标签: php fork command-line-interface pcntl undefined-function


【解决方案1】:

在将脚本作为 Apache 的一部分运行时,我遇到了同样的问题。所以,我的解决方案是将包含pcntl_fork 的代码放在不同的文件中(我们称之为fork.php)并使用exec() 运行它,如下所示:

mainFile.php(这是 Apache 将运行的)

exec('php fork.php',$output);

fork.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);
}

【讨论】:

    【解决方案2】:

    当 PHP 用作 Apache 模块(例如 XAMPP)时,不能使用函数 'pcntl_fork'。您只能在 CGI 模式或命令行中使用 pcntl_fork。

    使用这个函数会导致:'Fatal error: Call to undefined function: pcntl_fork()'

    来源:http://php.net/manual/en/function.pcntl-fork.php

    【讨论】:

    • 除此之外,如果从网页运行,OP 应考虑curl_multi_exec
    【解决方案3】:

    pcntl_* 函数,默认情况下未启用 PHP 中的过程控制支持。在编译 PHP 以启用过程控制支持时,您必须使用 --enable-pcntl 配置选项编译 PHP 的 CGI 或 CLI 版本(不用作 Apache 模块)。

    目前,此模块无法在非 Unix 平台 (Windows) 上运行。

    ref

    【讨论】:

      【解决方案4】:

      要查看是否已安装,请运行:

      php -i | grep pcntl

      如果它存在并启用,那么 pcntl 功能可能会被禁用,这似乎是较新的 PHP 5.x 安装中的默认设置。要检查,请运行:

      php -i | grep disable_functions

      如果您看到 pcntl_* 函数列表,则需要编辑 php.ini 文件(在 XAMPP 内部)并注释掉 disable_functions=

      我建议您使用适用于 OS X 的 PHP 的 this distribution,它有当前版本,我可以确认确实有 pcntl 扩展。

      【讨论】:

      • 注释掉“disabled_functions =”对我来说是不行的。 #ubuntu1404 #php7.1
      • disable_functions 返回warning 关于安全原因; fatal 没有关于未定义函数的错误。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-21
      • 2017-04-06
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 2011-01-26
      • 1970-01-01
      相关资源
      最近更新 更多