使用Workerman的Timer类,可以定时执行某些任务。
1. 建立app/test/command/Hello2.php。
<?php
declare (strict_types = 1);
namespace app\test\command;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use Workerman\Worker;
use Workerman\Lib\Timer;
class Hello2 extends Command
{
protected function configure()
{
// 指令配置
$this->setName('hello2')
->setDescription('the hello2 command');
}
protected function execute(Input $input, Output $output)
{
$worker = new Worker();
$worker->name = $this->getName();
$worker->onWorkerStart = function() use ($output)
{
// 每隔3秒钟执行一次
Timer::add(3, array($this, 'sayHello'), array($output));
};
Worker::runAll();
}
public function sayHello(Output $output)
{
$output->writeln('Hello ' . date('Y-m-d H:i:s'));
}
}
2. 配置config\console.php。
<?php
return [
'commands' => [
'app\test\command\Hello2',
],
];
3. 测试命令执行结果。
分类:
编程开发 - PHP
, 开源项目 - ThinkPHP
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
2014-05-10 How to authenticate a user by uid and password?
2014-05-10 LDAP 中 CN,OU,DC 的含意
2014-05-10 Apache Directory Studio
2014-05-10 LDAP编辑器 LDAPAdmin
2013-05-10 Windows XP下获取OpenERP源码
2013-05-10 关于OpenERP客户端字体问题的一点心得