add
int \Workerman\Lib\Timer::add(float $time_interval, callable $callback [,$args = array(), bool $persistent = true])
定時執行某個函數或者類方法
參數
time_interval
多長時間執行一次,機關秒,支援小數,可以精确到0.001,即精确到毫秒級别。
callback
回調函數注意:如果回調函數是類的方法,則方法必須是public屬性
args
回調函數的參數,必須為數組,數組元素為參數值
persistent
是否是持久的,如果隻想定時執行一次,則傳遞false(隻執行一次的任務在執行完畢後會自動銷毀,不必調用Timer::del())。預設是true,即一直定時執行。
傳回值
傳回一個整數,代表計時器的timerid,可以通過調用Timer::del($timerid)銷毀這個計時器。
示例
1、定時函數為匿名函數(閉包)
use \Workerman\Worker;
use \Workerman\Lib\Timer;
require_once ‘./Workerman/Autoloader.php‘;
$task = new Worker();
// 開啟多少個程序運作定時任務,注意多程序并發問題
$task->count = 1;
$task->onWorkerStart = function($task)
{
// 每2.5秒執行一次
$time_interval = 2.5;
Timer::add($time_interval, function()
{
echo "task run\n";
});
};
// 運作worker
Worker::runAll();
2、定時函數為普通函數
require_once ‘./Workerman/Autoloader.php‘;
use \Workerman\Worker;
use \Workerman\Lib\Timer;
// 普通的函數
function send_mail($to, $content)
{
echo "send mail ...\n";
}
$task = new Worker();
$task->onWorkerStart = function($task)
{
$to = ‘[email protected]‘;
$content = ‘hello workerman‘;
// 10秒後執行發送郵件任務,最後一個參數傳遞false,表示隻運作一次
Timer::add(10, ‘send_mail‘, array($to, $content), false);
};
// 運作worker
Worker::runAll();
3、定時函數為類的方法
require_once ‘./Workerman/Autoloader.php‘;
use \Workerman\Worker;
use \Workerman\Lib\Timer;
class Mail
{
// 注意,回調函數屬性必須是public
public function send($to, $content)
{
echo "send mail ...\n";
}
}
$task = new Worker();
$task->onWorkerStart = function($task)
{
// 10秒後發送一次郵件
$mail = new Mail();
$to = ‘[email protected]‘;
$content = ‘hello workerman‘;
Timer::add(10, array($mail, ‘send‘), array($to, $content), false);
};
// 運作worker
Worker::runAll();
4、定時函數為類方法(類内部使用定時器)
require_once ‘./Workerman/Autoloader.php‘;
use \Workerman\Worker;
use \Workerman\Lib\Timer;
class Mail
{
// 注意,回調函數屬性必須是public
public function send($to, $content)
{
echo "send mail ...\n";
}
public function sendLater($to, $content)
{
// 回調的方法屬于目前的類,則回調數組第一個元素為$this
Timer::add(10, array($this, ‘send‘), array($to, $content), false);
}
}
$task = new Worker();
$task->onWorkerStart = function($task)
{
// 10秒後發送一次郵件
$mail = new Mail();
$to = ‘[email protected]‘;
$content = ‘hello workerman‘;
$mail->sendLater($to, $content);
};
// 運作worker
Worker::runAll();
5、定時函數為類的靜态方法
require_once ‘./Workerman/Autoloader.php‘;
use \Workerman\Worker;
use \Workerman\Lib\Timer;
class Mail
{
// 注意這個是靜态方法,回調函數屬性也必須是public
public static function send($to, $content)
{
echo "send mail ...\n";
}
}
$task = new Worker();
$task->onWorkerStart = function($task)
{
// 10秒後發送一次郵件
$to = ‘[email protected]‘;
$content = ‘hello workerman‘;
// 定時調用類的靜态方法
Timer::add(10, array(‘Mail‘, ‘send‘), array($to, $content), false);
};
// 運作worker
Worker::runAll();
6、定時函數為類的靜态方法(帶命名空間)
namespace Task;
require_once ‘./Workerman/Autoloader.php‘;
use \Workerman\Worker;
use \Workerman\Lib\Timer;
class Mail
{
// 注意這個是靜态方法,回調函數屬性也必須是public
public static function send($to, $content)
{
echo "send mail ...\n";
}
}
$task = new Worker();
$task->onWorkerStart = function($task)
{
// 10秒後發送一次郵件
$to = ‘[email protected]‘;
$content = ‘hello workerman‘;
// 定時調用帶命名空間的類的靜态方法
Timer::add(10, array(‘\Task\Mail‘, ‘send‘), array($to, $content), false);
};
// 運作worker
Worker::runAll();
7、定時器中銷毀目前定時器(use閉包方式傳遞$timer_id)
use \Workerman\Worker;
use \Workerman\Lib\Timer;
require_once ‘./Workerman/Autoloader.php‘;
$task = new Worker();
$task->onWorkerStart = function($task)
{
// 計數
$count = 1;
// 要想$timer_id能正确傳遞到回調函數内部,$timer_id前面必須加位址符 &
$timer_id = Timer::add(1, function()use(&$timer_id, &$count)
{
echo "Timer run $count\n";
// 運作10次後銷毀目前定時器
if($count++ >= 10)
{
echo "Timer::del($timer_id)\n";
Timer::del($timer_id);
}
});
};
// 運作worker
Worker::runAll();
8、定時器中銷毀目前定時器(參數方式傳遞$timer_id)
require_once ‘./Workerman/Autoloader.php‘;
use \Workerman\Worker;
use \Workerman\Lib\Timer;
class Mail
{
public function send($to, $content, $timer_id)
{
// 臨時給目前對象添加一個count屬性,記錄定時器運作次數
$this->count = empty($this->count) ? 1 : $this->count;
// 運作10次後銷毀目前定時器
echo "send mail {$this->count}...\n";
if($this->count++ >= 10)
{
echo "Timer::del($timer_id)\n";
Timer::del($timer_id);
}
}
}
$task = new Worker();
$task->onWorkerStart = function($task)
{
$mail = new Mail();
// 要想$timer_id能正确傳遞到回調函數内部,$timer_id前面必須加位址符 &
$timer_id = Timer::add(1, array($mail, ‘send‘), array(‘to‘, ‘content‘, &$timer_id));
};
// 運作worker
Worker::runAll();
9、隻在指定程序中設定定時器
一個worker執行個體有4個程序,隻在id編号為0的程序上設定定時器。
use Workerman\Worker;
use Workerman\Lib\Timer;
require_once ‘./Workerman/Autoloader.php‘;
$worker = new Worker();
$worker->count = 4;
$worker->onWorkerStart = function($worker)
{
// 隻在id編号為0的程序上設定定時器,其它1、2、3号程序不設定定時器
if($worker->id === 0)
{
Timer::add(1, function(){
echo "4個worker程序,隻在0号程序設定定時器\n";
});
}
};
// 運作worker
Worker::runAll();