1

我想将 amphp/parallel 库用于非阻塞进程。我有一个简单的下载文件功能,它对远程图像文件进行 curl 命中并将其保存到本地。我正在通过 REST API 使用这种方法。基本上我想要一个应该在后端完成aysnc下载的过程,可以说是,REST API命中了函数,函数说“嘿,好的,我正在后台下载,你可以继续”。意味着非阻塞和 API 得到响应为 ok ,而不是等待。同时,如果下载时出现网络故障,worker 可以在一段时间后重新启动进程。我该如何开始?

我尝试了以下代码,但没有奏效。

require_once "vendor/autoload.php";
use Amp\Loop;
use Amp\Parallel\Worker\CallableTask;
use Amp\Parallel\Worker\DefaultWorkerFactory;

\Amp\Loop::run(function () {
  $remote_file_url = "some remote image url"; //http://example.com/some.png
  $file_save_path = "save path for file"; //var/www/html/some.png
  $factory = new DefaultWorkerFactory();
  $worker = $factory->create();
  $result = yield $worker->enqueue(new CallableTask('downloadFile', [$remote_file_url, $file_save_path]));

  $code = yield $worker->shutdown();

  });

//downloadFile是一个简单的下载函数

function downloadFile($remoteFile, $localFile) {
    if (!$remoteFile || !$localFile) {
        return;
    }
    set_time_limit(0);
    $fp = fopen($localFile, 'w+');
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $remoteFile);
    curl_setopt($ch, CURLOPT_TIMEOUT, 50);
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    $result = curl_exec($ch);
    curl_close($ch);
    fclose($fp);

    return $result ? true : false;
}

我收到此错误:

PHP Fatal error:  Uncaught Amp\\Parallel\\Worker\\TaskError: Uncaught Error in worker with message "Call to undefined function downloadFile()" and code "0" in /var/www/html/test/vendor/amphp/parallel/lib/Worker/Internal/TaskFailure.php:45\nStack trace:\n#0 /var/www/html/test/vendor/amphp/parallel/lib/Worker/TaskWorker.php(126): Amp\\Parallel\\Worker\\Internal\\TaskFailure->promise()\n#1 [internal function]: Amp\\Parallel\\Worker\\TaskWorker->Amp\\Parallel\\Worker\\{closure}()\n#2 /var/www/html/test/vendor/amphp/amp/lib/Coroutine.php(76): Generator->send(Object(Amp\\Parallel\\Worker\\Internal\\TaskFailure))\n#3 /var/www/html/test/vendor/amphp/amp/lib/Internal/Placeholder.php(130): Amp\\Coroutine->Amp\\{closure}(NULL, Object(Amp\\Parallel\\Worker\\Internal\\TaskFailure))\n#4 /var/www/html/test/vendor/amphp/amp/lib/Coroutine.php(81): Amp\\Coroutine->resolve(Object(Amp\\Parallel\\Worker\\Internal\\TaskFailure))\n#5 /var/www/html/test/vendor/amphp/amp/lib/Internal/Placeholder.php(130): Amp\\Coroutine->Amp\\{closure}(NULL, Object(Amp\\Parallel\\Worker\\Internal\\TaskFailur in /var/www/html/test/vendor/amphp/parallel/lib/Worker/Internal/TaskFailure.php on line 45

我有类似的要求,如amphp 如何在后台运行进程中工作。

4

1 回答 1

2

通常,Amp 不会在后台神奇地工作。如果您通过 PHP-FPM 或类似方式使用 PHP,则一旦响应完成,Amp 将被关闭,就像其他任何事情一样。

如果您想将这些请求中的工作转移到后台进程中,您需要某种队列(例如 beanstalkd)和一个(永久)工作人员来处理这些排队的工作。您可以使用 Amp 编写这样一个守护进程,但它必须在带外启动。

也就是说,如果您只想并发下载amphp/artax比使用 更适合amphp/parallel,因为与每个 HTTP 请求的单独 PHP 进程相比,它的开销要低得多。

于 2019-06-14T06:12:59.747 回答