簡體   English   中英

amphp 自動加載 class 未按預期工作

[英]amphp auto-load class not working as expected

我正在嘗試在使用 amphp 的工作人員中使用自定義 class ,但它似乎不起作用。 下面的 class 已經使用 composer 自動加載。 請幫我解決這個問題。 我的代碼如下:

Class(這實現了他們文檔中提到的任務):

<?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 分配使用上述 class 的工人的文件:

    <?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);
});

運行這個腳本給了我下面的 output:

[26-May-2021 01:23:11 UTC] PHP 致命錯誤:未捕獲的 Amp\Parallel\Worker\TaskFailureError:工作人員中未捕獲的錯誤,並顯示消息“用戶定義的函數必須是可自動加載的(即,在由作曲家);無法加載 function 'testMe'”和代碼“0”; 使用 Amp\Parallel\Worker\TaskFailureError::getOriginalTrace() 在 /home/xxxx/vendor/amphp/parallel/lib/Worker/Internal/TaskFailure.php:60 中的工作人員中的堆棧跟蹤

非常感謝您的閱讀!

這里的問題是你通過'testMe'然后檢查if (!\is_callable($this->function)) { ,而它應該是if (,\method_exists($this, $this->function)) { .

return ($this->function)(...$this->args); 應該是return ($this->{$this->function})(...$this->args); 如果您嘗試調用該方法。 您也可以直接調用該方法,而不是將其提供給構造函數。

如果你在 worker 中所做的一切都是 HTTP 請求,你應該查看amphp/http-client而不是amphp/parallel ,因為非阻塞 I/O 比幾個具有阻塞 I/O 的 chlid 進程更有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM