liaoxx 最近的时间轴更新
liaoxx

liaoxx

V2EX 第 464988 号会员,加入于 2020-01-14 14:07:20 +08:00
今日活跃度排名 14655
liaoxx 最近回复了
Trait ?
有幸写过一个用 yeild 和 guzzle/http 的请求池写过一个工具类
/**
* 异步/并发/批量请求 (慎用)
* @param string $url 请求地址
* @param array $allParams 所有请求参数 int[{once request params}]
* @param array $options 请求配置 ['headers'=>[]]
* @param callable|null $onSuccess 请求成功回调函数 function(ResponseInterface $response,int $index,array $params){}
* @param callable|null $onFailure 请求失败回调函数 function(\Exception $exception,int $index,array $params){}
* @param callable|null $onComplete 请求完成回调函数
* @return void
* @author LiaoYongjian
* @date 2024-01-19 17:43
*/
public function postAsyncRequests(string $url, array $allParams, array $options = [], callable $onSuccess = null, callable $onFailure = null, callable $onComplete = null)
{
$headers = $options['headers'] ?? [];
$client = $this->getCli();

//生成器 用于生成异步请求
$generator = function () use ($client, $allParams, $url, $headers, $onSuccess, $onFailure) {
foreach ($allParams as $index => $params) {
yield function () use ($client, $url, $headers, $index, $params, $onSuccess, $onFailure) {
//异步请求
return $client->postAsync($url, [
'headers' => $headers,
'json' => $params,
])->then(
function ($response) use ($onSuccess, $index, $params) {
if ($onSuccess) {
$onSuccess($response, $index, $params);
}
return $response;
},
function (\Exception $e) use ($onFailure, $index, $params) {
if ($onFailure) {
$onFailure($e, $index, $params);
}
throw $e;
}
);
};
}
};
//异步请求池
$pool = new Pool($client, $generator());
//异步请求池的回调函数
$responses = $pool->promise()->wait();
if ($onComplete){
$onComplete($responses);
}
}
141 天前
回复了 csulyb 创建的主题 PHP PHPer 现在写后台业务 实现高并发只有 swoole 吗
@jowan 我这里也在做短剧,并发有时候能上到 13w / s , 在我来这里上班之前 swoole 常驻内存,数据库连接池,持久化也尝试过,现在还是 php-fpm 这一套求稳
2022-02-18 19:29:04 +08:00
回复了 SeaRecluse 创建的主题 职场话题 十年跳八次,你会如何评价呢?
哈哈,不瞒您说写代码 3 年半,六家企业上过班,两次试用跑路,现在准备跑路
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   881 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 11ms · UTC 21:47 · PVG 05:47 · LAX 14:47 · JFK 17:47
Developed with CodeLauncher
♥ Do have faith in what you're doing.