add

    注意:定时器是在当前进程中运行的,workerman中不会创建新的进程或者线程去运行定时器。

    多长时间执行一次,单位秒,支持小数,可以精确到0.001,即精确到毫秒级别。

    回调函数注意:如果回调函数是类的方法,则方法必须是public属性

    args

    回调函数的参数,必须为数组,数组元素为参数值

    是否是持久的,如果只想定时执行一次,则传递false(只执行一次的任务在执行完毕后会自动销毁,不必调用Timer::del())。默认是true,即一直定时执行。

    返回值

    返回一个整数,代表计时器的timerid,可以通过调用Timer::del($timerid)销毁这个计时器。

    示例

    1、定时函数为匿名函数(闭包)

    1. use \Workerman\Worker;
    2. use \Workerman\Lib\Timer;
    3. require_once __DIR__ . '/Workerman/Autoloader.php';
    4. $task = new Worker();
    5. // 开启多少个进程运行定时任务,注意业务是否在多进程有并发问题
    6. $task->count = 1;
    7. $task->onWorkerStart = function($task)
    8. {
    9. // 每2.5秒执行一次
    10. $time_interval = 2.5;
    11. Timer::add($time_interval, function()
    12. {
    13. echo "task run\n";
    14. });
    15. };
    16. // 运行worker
    17. Worker::runAll();

    2、只在指定进程中设置定时器

    一个worker实例有4个进程,只在id编号为0的进程上设置定时器。

    1. use Workerman\Worker;
    2. use Workerman\Lib\Timer;
    3. require_once __DIR__ . '/Workerman/Autoloader.php';
    4. $worker = new Worker();
    5. $worker->count = 4;
    6. $worker->onWorkerStart = function($worker)
    7. {
    8. // 只在id编号为0的进程上设置定时器,其它1、2、3号进程不设置定时器
    9. if($worker->id === 0)
    10. {
    11. Timer::add(1, function(){
    12. echo "4个worker进程,只在0号进程设置定时器\n";
    13. });
    14. }
    15. };
    16. // 运行worker
    17. Worker::runAll();
    1. use \Workerman\Worker;
    2. use \Workerman\Lib\Timer;
    3. require_once __DIR__ . '/Workerman/Autoloader.php';
    4. $ws_worker = new Worker('websocket://0.0.0.0:8080');
    5. $ws_worker->count = 8;
    6. // 连接建立时给对应连接设置定时器
    7. $ws_worker->onConnect = function($connection)
    8. {
    9. // 每10秒执行一次
    10. $time_interval = 10;
    11. $connect_time = time();
    12. // 给connection对象临时添加一个timer_id属性保存定时器id
    13. $connection->timer_id = Timer::add($time_interval, function()use($connection, $connect_time)
    14. {
    15. $connection->send($connect_time);
    16. });
    17. };
    18. // 连接关闭时,删除对应连接的定时器
    19. $ws_worker->onClose = function($connection)
    20. {
    21. // 删除定时器
    22. Timer::del($connection->timer_id);
    23. };
    24. // 运行worker

    4、定时器函数为匿名函数,利用定时器接口传递参数

    5、定时函数为普通函数

    1. use \Workerman\Worker;
    2. use \Workerman\Lib\Timer;
    3. require_once __DIR__ . '/Workerman/Autoloader.php';
    4. // 普通的函数
    5. {
    6. echo "send mail ...\n";
    7. }
    8. $task = new Worker();
    9. $task->onWorkerStart = function($task)
    10. {
    11. $to = 'workerman@workerman.net';
    12. $content = 'hello workerman';
    13. // 10秒后执行发送邮件任务,最后一个参数传递false,表示只运行一次
    14. Timer::add(10, 'send_mail', array($to, $content), false);
    15. };
    16. // 运行worker
    17. Worker::runAll();

    6、定时函数为类的方法

    1. use \Workerman\Worker;
    2. use \Workerman\Lib\Timer;
    3. require_once __DIR__ . '/Workerman/Autoloader.php';
    4. class Mail
    5. {
    6. // 注意,回调函数属性必须是public
    7. public function send($to, $content)
    8. {
    9. echo "send mail ...\n";
    10. }
    11. }
    12. $task = new Worker();
    13. $task->onWorkerStart = function($task)
    14. {
    15. // 10秒后发送一次邮件
    16. $mail = new Mail();
    17. $to = 'workerman@workerman.net';
    18. $content = 'hello workerman';
    19. Timer::add(10, array($mail, 'send'), array($to, $content), false);
    20. };
    21. // 运行worker
    22. Worker::runAll();
    1. use \Workerman\Worker;
    2. use \Workerman\Lib\Timer;
    3. require_once __DIR__ . '/Workerman/Autoloader.php';
    4. class Mail
    5. {
    6. // 注意,回调函数属性必须是public
    7. public function send($to, $content)
    8. {
    9. echo "send mail ...\n";
    10. }
    11. public function sendLater($to, $content)
    12. {
    13. // 回调的方法属于当前的类,则回调数组第一个元素为$this
    14. Timer::add(10, array($this, 'send'), array($to, $content), false);
    15. }
    16. }
    17. $task = new Worker();
    18. $task->onWorkerStart = function($task)
    19. {
    20. // 10秒后发送一次邮件
    21. $mail = new Mail();
    22. $to = 'workerman@workerman.net';
    23. $content = 'hello workerman';
    24. $mail->sendLater($to, $content);
    25. };
    26. // 运行worker
    27. Worker::runAll();

    8、定时函数为类的静态方法

    9、定时函数为类的静态方法(带命名空间)

    1. use \Workerman\Worker;
    2. use \Workerman\Lib\Timer;
    3. require_once __DIR__ . '/Workerman/Autoloader.php';
    4. class Mail
    5. {
    6. // 注意这个是静态方法,回调函数属性也必须是public
    7. public static function send($to, $content)
    8. {
    9. echo "send mail ...\n";
    10. }
    11. }
    12. $task = new Worker();
    13. $task->onWorkerStart = function($task)
    14. {
    15. // 10秒后发送一次邮件
    16. $to = 'workerman@workerman.net';
    17. $content = 'hello workerman';
    18. // 定时调用带命名空间的类的静态方法
    19. Timer::add(10, array('\Task\Mail', 'send'), array($to, $content), false);
    20. };
    21. // 运行worker
    22. Worker::runAll();

    10、定时器中销毁当前定时器(use闭包方式传递$timer_id)

    1. use \Workerman\Worker;
    2. use \Workerman\Lib\Timer;
    3. require_once __DIR__ . '/Workerman/Autoloader.php';
    4. $task = new Worker();
    5. $task->onWorkerStart = function($task)
    6. {
    7. // 计数
    8. $count = 1;
    9. // 要想$timer_id能正确传递到回调函数内部,$timer_id前面必须加地址符 &
    10. $timer_id = Timer::add(1, function()use(&$timer_id, &$count)
    11. {
    12. echo "Timer run $count\n";
    13. // 运行10次后销毁当前定时器
    14. if($count++ >= 10)
    15. {
    16. echo "Timer::del($timer_id)\n";
    17. Timer::del($timer_id);
    18. }
    19. });
    20. };
    21. // 运行worker
    22. Worker::runAll();
    1. use \Workerman\Worker;
    2. use \Workerman\Lib\Timer;
    3. require_once __DIR__ . '/Workerman/Autoloader.php';
    4. class Mail
    5. {
    6. public function send($to, $content, $timer_id)
    7. {
    8. // 临时给当前对象添加一个count属性,记录定时器运行次数
    9. $this->count = empty($this->count) ? 1 : $this->count;
    10. // 运行10次后销毁当前定时器
    11. echo "send mail {$this->count}...\n";
    12. if($this->count++ >= 10)
    13. {
    14. echo "Timer::del($timer_id)\n";
    15. Timer::del($timer_id);
    16. }
    17. }
    18. }
    19. $task = new Worker();
    20. $task->onWorkerStart = function($task)
    21. {
    22. $mail = new Mail();
    23. // 要想$timer_id能正确传递到回调函数内部,$timer_id前面必须加地址符 &
    24. $timer_id = Timer::add(1, array($mail, 'send'), array('to', 'content', &$timer_id));
    25. };
    26. Worker::runAll();