您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

workerman定时器使用(附示例代码)

2026/1/2 5:06:17发布13次查看
下面由workerman php教程栏目给大家介绍workerman定时器使用方法,希望对需要的朋友有所帮助!
workerman是一个高性能的socket服务器通讯框架,用于快速开发各种网络应用,包括tcp的、udp的、长连接、短连接应用。下面我们来介绍一下workerman定时器使用。
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;    });};// 运行workerworker::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 = 'workerman@workerman.net';    $content = 'hello workerman';    // 10秒后执行发送邮件任务,最后一个参数传递false,表示只运行一次    timer::add(10, 'send_mail', array($to, $content), false);};// 运行workerworker::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 = 'workerman@workerman.net';    $content = 'hello workerman';    timer::add(10, array($mail, 'send'), array($to, $content), false);};// 运行workerworker::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 = 'workerman@workerman.net';    $content = 'hello workerman';    $mail->sendlater($to, $content);};// 运行workerworker::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 = 'workerman@workerman.net';    $content = 'hello workerman';    // 定时调用类的静态方法    timer::add(10, array('mail', 'send'), array($to, $content), false);};// 运行workerworker::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 = 'workerman@workerman.net';    $content = 'hello workerman';    // 定时调用带命名空间的类的静态方法    timer::add(10, array('\task\mail', 'send'), array($to, $content), false);};// 运行workerworker::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);        }    });};// 运行workerworker::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));};// 运行workerworker::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;        });    }};// 运行workerworker::runall();
更多workerman知识请关注workerman教程栏目。
以上就是workerman定时器使用(附示例代码)的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product