需要3秒請求一次 API接口,不管3秒內(nèi)是否請求到,必須在上一次請求的3秒后再次發(fā)出請求。
第1次請求--------2024-06-03 01:32:28
第2次請求--------2024-06-03 01:32:31
第3次請求--------2024-06-03 01:32:34
第4次請求--------2024-06-03 01:32:37
。
。
。
目前使用的是Crontab結(jié)合異步請求。
疑問:請問大佬們,這種方案會出現(xiàn)什么異常,如:連接過多或者跑滿內(nèi)存等問題?
代碼1如下
<?php
use Workerman\Worker;
require __DIR__ . '/vendor/autoload.php';
use Workerman\Crontab\Crontab;
$worker = new Worker();
// 設(shè)置時區(qū),避免運行結(jié)果與預期不一致
date_default_timezone_set('PRC');
$worker->onWorkerStart = function () {
// 每分鐘的第1秒執(zhí)行.
$http = new Workerman\Http\Client();
new Crontab('*/3 * * * * *', function() use ($http){
$http->request('http://www.wtbis.cn', [
'method' => 'POST',
'version' => '1.1',
'headers' => ['content-type' => 'application/json','Connection' => 'keep-alive'],
'data' => json_encode(['detail' => true]),
'success' => function ($response) {
$result = $response->getBody();
$result = json_decode($response->getBody(),true);
echo date('Y-m-d H:i:s')."\n";
},
'error' => function ($exception) {
echo $exception;
}
]);
});
};
Worker::runAll();
代碼2如下
<?php
use Workerman\Worker;
use Workerman\Timer;
require_once __DIR__ . '/vendor/autoload.php';
//發(fā)送請求
function request($http)
{
$http->request('http://www.wtbis.cn/', [
'method' => 'POST',
'version' => '1.1',
'headers' => ['content-type' => 'application/json','Connection' => 'keep-alive'],
'data' => json_encode(['detail' => true]),
'success' => function ($response) {
$result = $response->getBody();
$result = json_decode($response->getBody(),true);
},
'error' => function ($exception) {
echo $exception;
}
]);
}
$task = new Worker();
// 開啟多少個進程運行定時任務,注意業(yè)務是否在多進程有并發(fā)問題
$task->count = 1;
$task->onWorkerStart = function(Worker $task)
{
$http = new Workerman\Http\Client();
Timer::add(3, 'request',[$http],true);
};
// 運行worker
Worker::runAll();
那你不應該使用定時器 應該使用Timer::add() persistent參數(shù)傳false;
$worker->onWorkerStart = function () {
$cb = function()use(&$cb){
$http = new Workerman\Http\Client();
$http->request('http://www.wtbis.cn', [
'method' => 'POST',
'version' => '1.1',
'headers' => ['content-type' => 'application/json','Connection' => 'keep-alive'],
'data' => json_encode(['detail' => true]),
'success' => function ($response) {
$result = $response->getBody();
$result = json_decode($response->getBody(),true);
echo date('Y-m-d H:i:s')."\n";
Timer::add(3,$cb,[],false);
},
'error' => function ($exception) {
echo $exception;
Timer::add(3,$cb,[],false);
}
]);
}
$cb();
};
Worker::runAll();
我是這樣想的,你可以嘗試一下是否能夠滿足需求,我并沒有實際使用過這種方式
這段代碼的意思就是請求后3s后再次執(zhí)行$cb方法,persistent-false表示定時器不循環(huán)執(zhí)行,就是執(zhí)行一次的意思
這個你可以自己進行計算的嘛 比如開始獲取一下$start = time() 然后請求后獲取一下time()-$start
if(time()-$start >=3){
Timer::add(0.001,$cb,[],false); //直接執(zhí)行下一次
}else{
Timer::add(3-(time()-$start),$cb,[],false); //執(zhí)行下一次
}
如果一定的3s執(zhí)行 那就不管http請求唄 反正Workerman\Http\Client
本來就是異步的
你就Timer::add(3,$cb) 發(fā)送一個請求就完了 不管請求執(zhí)行了幾秒
那其實你現(xiàn)在的邏輯并沒問題,就是3s發(fā)送一次請求嘛 Workerman\Http\Client
是異步的 又不管時間,我理解錯了題意思,我以為是請求后才能在發(fā)送下一個請求