首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >amphp自动加载类无法按预期工作。

amphp自动加载类无法按预期工作。
EN

Stack Overflow用户
提问于 2021-05-26 01:22:53
回答 1查看 209关注 0票数 0

我试图在使用amphp的工作人员中使用自定义类,但它似乎不起作用。下面的类已经使用composer自动加载.请帮我解决这个问题。我的代码如下:

类(这实现了在其文档中提到的任务):

代码语言:javascript
复制
<?php

namespace Jobs;

require '/home/xxx/vendor/autoload.php';

use Amp\Parallel\Worker\Environment;
use Amp\Parallel\Worker\Task;

class GetMarketJob implements Task {
    /**
     * @var callable
     */
    private $function;
    /**
     * @var mixed[]
     */
    private $args;

    public function __construct($function, ...$args) {
        $this->function = $function;
        $this->args = $args;
    }

    /**
     * {@inheritdoc}
     */
    public function run(Environment $environment)
    {
        if ($this->function instanceof \__PHP_Incomplete_Class) {
            throw new \Error('When using a class instance as a callable, the class must be autoloadable');
        }

        if (\is_array($this->callable) && ($this->callable[0] ?? null) instanceof \__PHP_Incomplete_Class) {
            throw new \Error('When using a class instance method as a callable, the class must be autoloadable');
        }

        if (!\is_callable($this->function)) {
            $message = 'User-defined functions must be autoloadable (that is, defined in a file autoloaded by composer)';
            if (\is_string($this->function)) {
                $message .= \sprintf("; unable to load function '%s'", $this->function);
            }

            throw new \Error($message);
        }

        return ($this->function)(...$this->args);
    }

    public function testMe($url = NULL) {
        $test = file_get_contents($url);
        return $test;
    }    
}

使用amphp分配使用上述类的工作人员的文件:

代码语言:javascript
复制
    <?php

require '/home/xxxx/vendor/autoload.php';

use Jobs\GetMarketJob;
// Example async producer using promisor

use Amp\Parallel\Worker;
use Amp\Promise;
use Amp\Loop;
use Amp\Parallel\Worker\DefaultPool;
use Amp\Parallel\Worker\Task;
use Amp\Parallel\Worker\Environment;
use Amp\Parallel\Worker\TaskFailureError;
use Amp\Parallel\Worker\DefaultWorkerFactory;

Amp\Loop::run(function () {
    $factory = new DefaultWorkerFactory();

    $worker = $factory->create();

    $result = yield $worker->enqueue(new GetMarketJob('testMe', ['https://www.syhtek.com']));
    
    print($result);

    $code = yield $worker->shutdown();
    \printf("Code: %d\n", $code);
});

运行此脚本将给出以下输出:

UTC致命错误: Uncaught Amp\并行\ worker \TaskFailureError:带有消息的工作人员中的未捕获错误“用户定义的函数必须是可自动加载的(即,在编写器自动加载的文件中定义的函数);无法加载函数‘testMe’和代码"0";在/home/xxxx/vendor/amphp/parallel/lib/Worker/Internal/TaskFailure.php:60中的工作程序中使用

作为堆栈跟踪

非常感谢你的阅读!

EN

回答 1

Stack Overflow用户

发布于 2021-07-08 19:47:14

这里的问题是,您正在传递'testMe',然后检查if (!\is_callable($this->function)) {,而它应该是if (!\method_exists($this, $this->function)) {

如果您试图调用该方法,那么return ($this->function)(...$this->args);应该是return ($this->{$this->function})(...$this->args);。您也可以直接调用该方法,而不是将其交给构造函数。

如果您在工作程序中所做的一切都是HTTP请求,则应该查看amphp/http-client而不是amphp/parallel,因为非阻塞I/O比几个阻塞I/O的chlid进程效率高得多。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67697403

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档