Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于异步并发请求,文档的使用demo 不完整 #4

Closed
z86823237 opened this issue Feb 15, 2023 · 2 comments
Closed

关于异步并发请求,文档的使用demo 不完整 #4

z86823237 opened this issue Feb 15, 2023 · 2 comments

Comments

@z86823237
Copy link

z86823237 commented Feb 15, 2023

你好,我在webman项目中引入了你的http库,然后发现http并发请求无法执行,或者是执行后callback没有执行。当php进程结束时,console才输出了所有的callback内容;

然后我去阅读源代码,我发现在 并发异步请求后,你是不是少了2行代码?我现在将它标记出来,你可以检测看是否缺失;

这是你的demo

$promises = [
            Http::getAsync('http://easyhttp.yzh52521.cn/api/sleep3.json'),
            Http::getAsync('http1://easyhttp.yzh52521.cn/api/sleep1.json', ['name' => 'yzh52521']),
            Http::postAsync('http://easyhttp.yzh52521.cn/api/sleep2.json', ['name' => 'yzh52521']),
        ];

Http::concurrency(3)->multiAsync($promises, function (Response $response, $index) {
    echo "发起第 $index 个异步请求,请求时长:" . $response->json()->second . '秒' . PHP_EOL;
}, function (RequestException $e, $index) {
    echo "发起第 $index 个请求失败,失败原因:" . $e->getMessage() . PHP_EOL;
});

我补充后的demo用法:

$promises = [
    Http::getAsync('http://easyhttp.yzh52521.cn/api/sleep3.json'),
    Http::getAsync('http1://easyhttp.yzh52521.cn/api/sleep1.json', ['name' => 'yzh52521']),
    Http::postAsync('http://easyhttp.yzh52521.cn/api/sleep2.json', ['name' => 'yzh52521']),
];

$pool = Http::concurrency(3)->multiAsync($promises, function (Response $response, $index) {
    echo "发起第 $index 个异步请求,请求时长:" . $response->json()->second . '秒' . PHP_EOL;
}, function (RequestException $e, $index) {
    echo "发起第 $index 个请求失败,失败原因:" . $e->getMessage() . PHP_EOL;
});

// 等待请求完成
$promise = $pool->promise();

// 强制结束请求
$promise->wait();
@z86823237 z86823237 changed the title 关于异步并发请求,文档的使用demo不完整 关于异步并发请求,文档的使用demo 不完整 Feb 15, 2023
@yuanzhihai
Copy link
Owner

感谢反馈

@z86823237
Copy link
Author

@yzh52521 然后同时我也发现了,并发请求并不是真正意义上的并发。他不是一瞬间所有请求都打出,还是有顺序的请求;
workerman-http 库是真做到异步请求,豪无阻塞的那种

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants