您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
workerman-crontab定时任务 - 接口-销毁定时器
发布时间:2021-12-31 21:55:17编辑:雪饮阅读()
接口
Crontab::destroy()
销毁定时器
实例:
<?php
use Workerman\Worker;
require __DIR__ . '/vendor/autoload.php';
use Workerman\Crontab\Crontab;
$worker = new Worker();
// 设置时区,避免运行结果与预期不一致
date_default_timezone_set('PRC');
$crontabInstance=null;
$worker->onWorkerStart = function () {
// 每分钟的第1秒执行.
new Crontab('1 * * * * *', function(){
global $crontabInstance;
echo date('Y-m-d H:i:s')."\n";
//销毁定时器
if($crontabInstance){
$crontabInstance->destroy();
}
});
};
Worker::runAll();
实例运行一会儿:
[root@localhost workerman]# php index.php start
Workerman[index.php] start in DEBUG mode
------------------------------------- WORKERMAN --------------------------------------
Workerman version:4.0.26 PHP version:7.3.31
-------------------------------------- WORKERS ---------------------------------------
proto user worker listen processes status
tcp root none none 1 [OK]
--------------------------------------------------------------------------------------
Press Ctrl+C to stop. Start success.
2021-12-31 15:45:01
关键字词:workerman,crontab,定时任务,销毁定时器,接口