duanaoyuan7202 2019-07-14 12:26
浏览 129

如何从PHP中获取Guzzle async http客户端的最快响应

I want to use Guzzle to send HTTP requests to multiple endpoints and I want to use the response that comes in first, rather than waiting for all requests to complete.

My code:

    $client = new \GuzzleHttp\Client();

    $p1 = $client->requestAsync('GET', 'slow.host');
    $p2 = $client->requestAsync('GET', 'fast.host');

    $any = \GuzzleHttp\Promise\any([$p1, $p2]);
    $response = $any->wait();

I was expecting that as soon as either of the promises ($p1, $p2) gets resolved, I would get a response, however that's not how it works with Guzzle. Guzzle will always wait for $p1 to either resolve or reject, even if it takes forever.

From the example above, if the slow.host takes 10 seconds to send a response, and the fast.host takes 1 second to send a response, I would have to wait 10 seconds anyway. I would get the response from fast.host only in case slow.host fails completely (promise gets rejected, no such host, etc).

How do I get the fastest response immediately and ignore the rest?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料