PHP 工作人员(多进程 and/or 线程)

PHP worker (Multiple processes and/or threads)

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

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

是否有任何基于 framework/library 的简单 PHP 用于 multi-process/pthread 和作业队列处理?我不需要像 symfony 或 laravel.

这样的大型框架

工人

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

您将不得不 运行 一个本地 Redis 服务器(也可以是远程服务器)。根据 this SO

,我相信你可以在 Windows 上 运行 Redis

并发请求数

您可能还想研究使用 GuzzleHttp, you can find examples on how to use that here

之类的方法发送并发请求

来自文档:

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

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');