PRipple協程引擎是一個100%原生PHP代碼實現的現代化協程引擎,不依賴任何擴展,
可以在任何PHP環(huán)境(FPM/CLI)下運行, 本組件是基于該引擎開發(fā)的GuzzleHttp的異步請求插件,
既保留了GuzzleHttp的所有功能(上傳/下載等),又支持協程異步請求
PHP
>= 8.1
Linux/Unix
系統
composer require cclilshy/p-ripple-drive
配置
config/server.php
文件
<?php
return [
//...
'event_loop' => \Psc\Drive\Workerman\PDrive::class,
];
//...
Worker::$eventLoop = \Psc\Drive\Workerman\PDrive::class;
public function index(Request $request): string
{
$handle = new \Psc\Plugins\Guzzle\PHandler([
'pool' => 1 // 是否啟用Http長連接
]);
$client = new \GuzzleHttp\Client(['handle' => $handle]);
// 發(fā)送請求(即使該請求耗時很長, 也不會堵塞當前進程的其他請求)
$response = $client->get('http://www.baidu.com');
return $response->getBody()->getContents();
}
public function index(Request $request): string
{
$client = \P\Plugin::Guzzle();
// 發(fā)送請求(即使該請求耗時很長, 也不會堵塞當前進程的其他請求)
$response = $client->get('http://www.baidu.com');
return $response->getBody()->getContents();
}
public function index(Request $request): string
{
for ($i = 0; $i < 100; $i++) {
\P\async(function () use ($i) {
$response = \P\Plugin::Guzzle()->get('https://www.qq.com/');
$time = microtime(true);
$responseStatusCode = $response->getStatusCode();
var_dump("[{$time}]request {$i} status: {$responseStatusCode}");
});
}
return 'hello,world';
}
剛才測試的時候,用了之前文章里的:
$response = \P\await(\P\Plugin::Guzzle()->getAsync('https://www.baidu.com'));
不知為何沒效果;
另外,
好像缺了很多訪問,我用的webman單進程測試的。。
試了一下:
for ($i = 0; $i < 30; $i++) {
\P\async(function () use ($i) {
try {
$response = \P\Plugin::Guzzle()->get('https://www.qq.com');
$time = microtime(true);
$responseStatusCode = $response->getStatusCode();
var_dump("[{$time}] request {$i} status: {$responseStatusCode}");
} catch (\Exception $e) {
var_dump($e->getMessage());
} catch (\Throwable $e) {
var_dump($e->getMessage());
} finally {
}
});
沒有捕獲到異常,仍然是丟了Request,有時能丟個70%左右。。。
你好,這個好像發(fā)送post 請求接收不到數據,是我使用方式有問題嗎
public function test11()
{
$data = ['name' => 'tom', 'age' => 10];
\P\async(function () use ($data) {
\P\Plugin::Guzzle()->post('http://127.0.0.1:18787/index/test12', [
'json' => $data,
]);
});
return 'ok';
}
public function test12(Request $request)
{
$res = $request->all();
var_dump($res);
return true;
}
public function test13()
{
$data = ['name' => 'tom', 'age' => 10];
$client = new \GuzzleHttp\Client();
$client->post('http://127.0.0.1:18787/index/test12', [
'json' => $data,
]);
}
這是頭部沒有自動加入content-length導致的,
已在下個版本解決,詳見
https://github.com/cloudtay/p-ripple-core/commit/f9cc669d9eeac09d44b2673a31140ba146470973
現在可以通過這個方式解決1
\P\async(function () {
$data = ['name' => 'tom', 'age' => 10];
$body = json_encode($data);
\P\Plugin::Guzzle()->post('http://127.0.0.1:8787/index/test12', [
'body' => $body,
'headers' => [
'Content-Type' => 'application/json',
'Content-Length' => strlen($body),
],
]);
});
牛啊,上面那個問題好了,測試發(fā)現協程發(fā)送超過1000個請求就會失敗,進程直接退出
public function test9(Request $request): string
{
for ($i = 0; $i < 1000; $i++) {
\P\async(function () use ($i) {
$response = \P\Plugin::Guzzle()->get('https://www.qq.com/');
$time = microtime(true);
$responseStatusCode = $response->getStatusCode();
var_dump("[{$time}]request {$i} status: {$responseStatusCode}");
});
}
return 'hello,world';
}
Error: Call to undefined method Psc\Plugins\Guzzle\Guzzle::get();這里的代碼跑不通,更新了.希望大佬可以改下嗎?
然后 官網文檔是打不開被墻了.希望多些代碼例子.
... 感覺這玩意 出生的有點晚了。 對于耗時的業(yè)務流程,現在常規(guī)做法都是丟redis隊列里面慢慢跑了..不過 感覺這個比隊列還是方便些,比如一些保存文件。之類的操作,那就可以直接把保存的路徑返回回去 后臺這邊慢慢保存文件了。 不需要東寫一句 西寫一句了