首页 > 解决方案 > 将 Amphp 与用户定义的函数并行使用时出错

问题描述

我正在尝试使用 Worker\enqueueCallable 调用用户定义的函数。
我已将该函数放在一个单独的文件中,并使用 composer 来自动加载它。

我得到了错误:

致命错误:未捕获的类型错误:传递给 Amp\Parallel\Worker\enqueueCallable() 的参数 1 必须是可调用的,给定字符串”

我的文件夹结构

Project Folder/
    app/
       funcs.php
       caller.php
    main.php
    composer.json

在caller.phpfuncs.php中都使用了命名空间应用程序

然后在 composer.json 中使用以下代码段并使用composer dumpautoload -o

{
    "autoload": {
        "psr-4": {
            "app\\": "app/"
        },
        "files": [ "app/funcs.php" ]
    },
    "require": {
        "amphp/parallel": "^1.3",
        "amphp/parallel-functions": "^0.1.3"
    }

} 

main.php我调用caller.php调用funcs.php中定义的函数

函数.php

namespace app;
function f1($param1){
  #Do some stuff 
}

调用者.php

namespace app;

use Amp\Parallel\Worker;
use Amp\Promise;

class cls
{
    function __construct()
    {

    }

    function Invoker()
    {
        // If I call the function, its working.
        f1($param1);
        // When I am using its not working.
        $promises[] = Worker\enqueueCallable('f1', $param1);  
    }

}

主文件

require __DIR__ . '\vendor\autoload.php';
use app\cls;

$obj = new cls();
$obj->Invoker();

我正在使用 Windows-10、WAMP 服务器和 PHP 版本 7.3.5。

标签: phpcomposer-phpamphp

解决方案


推荐阅读