我正在尝试在使用 amphp 的工作人员中使用自定义类,但它似乎不起作用。下面的类已经使用 composer 自动加载。请帮我解决这个问题。我的代码如下:
类(这实现了他们文档中提到的任务):
<?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 文件分配使用上述类的工人:
<?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);
});
运行此脚本会给我以下输出:
[26-May-2021 01:23:11 UTC] PHP 致命错误:Uncaught Amp\Parallel\Worker\TaskFailureError: Uncaught Error in worker with message "User-defined functions must be autoloadable (that is, defined in a file autoloaded by作曲家);无法加载函数“testMe”和代码“0”;使用 Amp\Parallel\Worker\TaskFailureError::getOriginalTrace() 获取 /home/xxxx/vendor/amphp/parallel/lib/Worker/Internal/TaskFailure.php:60 中工作程序中的堆栈跟踪
非常感谢您的阅读!