PHP worker (多个进程和/或线程)


PHP worker (Multiple processes and/or threads)

我正在尝试从Web服务获取统计数据。每个请求的响应时间为 1-2 秒,我必须提交数千个 ID 的请求,一次一个。由于服务器的响应时间,所有请求的总和将达到几个小时。

我想并行化尽可能多的请求(服务器可以处理它)。我已经安装了 PHP7 和 pthreads(仅限 CLI),但最大线程数是有限的(在 Windows PHP CLI 中为 20),所以我必须启动多个进程。

是否有任何简单的基于 PHP 的框架/库用于多进程/pthread 和作业队列处理?我不需要像symfony或laravel这样的大框架。

工人

你可以考虑使用不需要pthreads的php-resque。

不过,您必须运行本地 Redis 服务器(也可以是远程的)。我相信你可以在Windows上运行Redis,根据这个SO

<小时 />

并发请求

您可能还想研究使用GuzzleHttp之类的东西发送并发请求,您可以在此处找到有关如何使用它的示例。

从文档中:

您可以使用承诺和异步请求同时发送多个请求。

use GuzzleHttp'Client;
use GuzzleHttp'Promise;
$client = new Client(['base_uri' => 'http://httpbin.org/']);
// Initiate each request but do not block
$promises = [
    'image' => $client->getAsync('/image'),
    'png'   => $client->getAsync('/image/png'),
    'jpeg'  => $client->getAsync('/image/jpeg'),
    'webp'  => $client->getAsync('/image/webp')
];
// Wait on all of the requests to complete. Throws a ConnectException
// if any of the requests fail
$results = Promise'unwrap($promises);
// Wait for the requests to complete, even if some of them fail
$results = Promise'settle($promises)->wait();
// You can access each result using the key provided to the unwrap
// function.
echo $results['image']->getHeader('Content-Length');
echo $results['png']->getHeader('Content-Length');