ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] ## [定时器使用注意事项](#) 1、只能在`onXXXX`回调中添加定时器。全局的定时器推荐在`onWorkerStart`回调中设置,针对某个连接的定时器推荐在`onConnect`中设置。 2、添加的定时任务在当前进程执行(不会启动新的进程或者线程),如果任务很重(特别是涉及到网络IO的任务),可能会导致该进程阻塞,暂时无法处理其它业务。所以最好将耗时的任务放到单独的进程运行,例如建立一个/多个Worker进程运行 3、当前进程忙于其它业务时或者当一个任务没有在预期的时间运行完,这时又到了下一个运行周期,则会等待当前任务完成才会运行,这会导致定时器没有按照预期时间间隔运行。也就是说当前进程的业务都是串行执行的,如果是多进程则进程间的任务运行是并行的。 4、需要注意多进程设置了定时任务造可能会造成并发问题,例如下面的代码每秒会打印5次。 ~~~php $worker = new Worker(); // 5个进程 $worker->count = 5; $worker->onWorkerStart = function(Worker $worker) { // 5个进程,每个进程都有一个这样的定时器 Timer::add(1, function(){ echo "hi\r\n"; }); }; Worker::runAll(); ~~~ 如果只想要一个进程运行定时器,参考[Timer::add 示例2](https://www.workerman.net/doc/workerman/timer/add.html) 5、可能会有1毫秒左右的误差。 6、定时器不能跨进程删除,例如a进程设置的定时器无法在b进程直接调用Timer::del接口删除 7、不同进程间的定时器id可能会重复,但是同一个进程内产生的定时器id不会重复 8、更改系统时间后会影响定时器的行为,所以更改系统时间后建议restart重启 ## **添加定时器** int \Workerman\Timer::add(float $time_interval执行间隔单位秒, callable $callback回调函数 [,$args回调参数 = array(), bool $persistent持久true一次false = true]) #### [1、定时函数为匿名函数(闭包)](#) ~~~php use Workerman\Worker; use Workerman\Timer; require_once __DIR__ . '/vendor/autoload.php'; $task = new Worker(); // 开启多少个进程运行定时任务,注意业务是否在多进程有并发问题 $task->count = 1; $task->onWorkerStart = function(Worker $task) { // 每2.5秒执行一次 $time_interval = 2.5; Timer::add($time_interval, function() { echo "task run\n"; }); }; // 运行worker Worker::runAll(); ~~~ ### [2、只在指定进程中设置定时器](#) 一个worker实例有4个进程,只在id编号为0的进程上设置定时器。 ~~~php use Workerman\Worker; use Workerman\Timer; require_once __DIR__ . '/vendor/autoload.php'; $worker = new Worker(); $worker->count = 4; $worker->onWorkerStart = function(Worker $worker) { // 只在id编号为0的进程上设置定时器,其它1、2、3号进程不设置定时器 if($worker->id === 0) { Timer::add(1, function(){ echo "4个worker进程,只在0号进程设置定时器\n"; }); } }; // 运行worker Worker::runAll(); ~~~ ### [3、定时函数为匿名函数,利用闭包传递参数](#) ~~~php use Workerman\Worker; use Workerman\Timer; use Workerman\Connection\TcpConnection; require_once __DIR__ . '/vendor/autoload.php'; $ws_worker = new Worker('websocket://0.0.0.0:8080'); $ws_worker->count = 8; // 连接建立时给对应连接设置定时器 $ws_worker->onConnect = function(TcpConnection $connection) { // 每10秒执行一次 $time_interval = 10; $connect_time = time(); // 给connection对象临时添加一个timer_id属性保存定时器id $connection->timer_id = Timer::add($time_interval, function()use($connection, $connect_time) { $connection->send($connect_time); }); }; // 连接关闭时,删除对应连接的定时器 $ws_worker->onClose = function(TcpConnection $connection) { // 删除定时器 Timer::del($connection->timer_id); }; // 运行worker Worker::runAll(); ~~~ ### [4、定时器函数为匿名函数,利用定时器接口传递参数](#) ~~~php use Workerman\Worker; use Workerman\Timer; use Workerman\Connection\TcpConnection; require_once __DIR__ . '/vendor/autoload.php'; $ws_worker = new Worker('websocket://0.0.0.0:8080'); $ws_worker->count = 8; // 连接建立时给对应连接设置定时器 $ws_worker->onConnect = function(TcpConnection $connection) { // 每10秒执行一次 $time_interval = 10; $connect_time = time(); // 给connection对象临时添加一个timer_id属性保存定时器id $connection->timer_id = Timer::add($time_interval, function($connection, $connect_time) { $connection->send($connect_time); }, array($connection, $connect_time)); }; // 连接关闭时,删除对应连接的定时器 $ws_worker->onClose = function(TcpConnection $connection) { // 删除定时器 Timer::del($connection->timer_id); }; // 运行worker Worker::runAll(); ~~~ ### [5、定时函数为普通函数](#) ~~~php use Workerman\Worker; use Workerman\Timer; require_once __DIR__ . '/vendor/autoload.php'; // 普通的函数 function send_mail($to, $content) { echo "send mail ...\n"; } $task = new Worker(); $task->onWorkerStart = function(Worker $task) { $to = 'workerman@workerman.net'; $content = 'hello workerman'; // 10秒后执行发送邮件任务,最后一个参数传递false,表示只运行一次 Timer::add(10, 'send_mail', array($to, $content), false); }; // 运行worker Worker::runAll(); ~~~ ### [6、定时函数为类的方法](#) ~~~php use Workerman\Worker; use Workerman\Timer; require_once __DIR__ . '/vendor/autoload.php'; class Mail { // 注意,回调函数属性必须是public public function send($to, $content) { echo "send mail ...\n"; } } $task = new Worker(); $task->onWorkerStart = function($task) { // 10秒后发送一次邮件 $mail = new Mail(); $to = 'workerman@workerman.net'; $content = 'hello workerman'; Timer::add(10, array($mail, 'send'), array($to, $content), false); }; // 运行worker Worker::runAll(); ~~~ ### [7、定时函数为类方法(类内部使用定时器)](https://www.workerman.net/doc/workerman/timer/add.html#7%E3%80%81%E5%AE%9A%E6%97%B6%E5%87%BD%E6%95%B0%E4%B8%BA%E7%B1%BB%E6%96%B9%E6%B3%95%EF%BC%88%E7%B1%BB%E5%86%85%E9%83%A8%E4%BD%BF%E7%94%A8%E5%AE%9A%E6%97%B6%E5%99%A8%EF%BC%89) ~~~php use Workerman\Worker; use Workerman\Timer; require_once __DIR__ . '/vendor/autoload.php'; 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(Worker $task) { // 10秒后发送一次邮件 $mail = new Mail(); $to = 'workerman@workerman.net'; $content = 'hello workerman'; $mail->sendLater($to, $content); }; // 运行worker Worker::runAll(); ~~~ ### [8、定时函数为类的静态方法](#) ~~~php use Workerman\Worker; use Workerman\Timer; require_once __DIR__ . '/vendor/autoload.php'; class Mail { // 注意这个是静态方法,回调函数属性也必须是public public static function send($to, $content) { echo "send mail ...\n"; } } $task = new Worker(); $task->onWorkerStart = function(Worker $task) { // 10秒后发送一次邮件 $to = 'workerman@workerman.net'; $content = 'hello workerman'; // 定时调用类的静态方法 Timer::add(10, array('Mail', 'send'), array($to, $content), false); }; // 运行worker Worker::runAll(); ~~~ ### [9、定时函数为类的静态方法(带命名空间)](#) ~~~php namespace Task; use Workerman\Worker; use Workerman\Timer; require_once __DIR__ . '/vendor/autoload.php'; class Mail { // 注意这个是静态方法,回调函数属性也必须是public public static function send($to, $content) { echo "send mail ...\n"; } } $task = new Worker(); $task->onWorkerStart = function(Worker $task) { // 10秒后发送一次邮件 $to = 'workerman@workerman.net'; $content = 'hello workerman'; // 定时调用带命名空间的类的静态方法 Timer::add(10, array('\Task\Mail', 'send'), array($to, $content), false); }; // 运行worker Worker::runAll(); ~~~ ### [10、定时器中销毁当前定时器(use闭包方式传递$timer\_id)](#) ~~~php use Workerman\Worker; use Workerman\Timer; require_once __DIR__ . '/vendor/autoload.php'; $task = new Worker(); $task->onWorkerStart = function(Worker $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(); ~~~ ### [11、定时器中销毁当前定时器(参数方式传递$timer\_id)](#) ~~~php use Workerman\Worker; use Workerman\Timer; require_once __DIR__ . '/vendor/autoload.php'; 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(Worker $task) { $mail = new Mail(); // 要想$timer_id能正确传递到回调函数内部,$timer_id前面必须加地址符 & $timer_id = Timer::add(1, array($mail, 'send'), array('to', 'content', &$timer_id)); }; // 运行worker Worker::runAll(); ~~~ ## **删除某个定时器** ### [示例](https://www.workerman.net/doc/workerman/timer/del.html#%E7%A4%BA%E4%BE%8B) ~~~php use Workerman\Worker; use Workerman\Timer; require_once __DIR__ . '/vendor/autoload.php'; $task = new Worker(); // 开启多少个进程运行定时任务,注意多进程并发问题 $task->count = 1; $task->onWorkerStart = function(Worker $task) { // 每2秒运行一次 $timer_id = Timer::add(2, function() { echo "task run\n"; }); // 20秒后运行一个一次性任务,删除2秒一次的定时任务 Timer::add(20, function($timer_id) { Timer::del($timer_id); }, array($timer_id), false); }; // 运行worker Worker::runAll(); ~~~ ### [实例(定时器回调中删除当前定时器)](https://www.workerman.net/doc/workerman/timer/del.html#%E5%AE%9E%E4%BE%8B(%E5%AE%9A%E6%97%B6%E5%99%A8%E5%9B%9E%E8%B0%83%E4%B8%AD%E5%88%A0%E9%99%A4%E5%BD%93%E5%89%8D%E5%AE%9A%E6%97%B6%E5%99%A8)) ~~~php use Workerman\Worker; use Workerman\Timer; use Workerman\Connection\TcpConnection; require_once __DIR__ . '/vendor/autoload.php'; $task = new Worker(); $task->onWorkerStart = function(Worker $task) { // 注意,回调里面使用当前定时器id必须使用引用(&)的方式引入 $timer_id = Timer::add(1, function()use(&$timer_id) { static $i = 0; echo $i++."\n"; // 运行10次后删除定时器 if($i === 10) { Timer::del($timer_id); } }); }; // 运行worker Worker::runAll(); ~~~