ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
#### 定时器处理 `Swoolefy\Core\Timer\TickManager`作为定时器管理类 1、循环定时器 ~~~ TickManager::getInstance()->tickTimer($time_interval, $func, $params = null) ~~~ 例如,创建一个循环定时器,返回定时$timer_id ~~~ $timer_id = TickManager::getInstance()->tickTimer(5000, ['App\Tick\TickController', 'test'], $params = '循环定时器') ~~~ 定时器处理程序,其实就是创建一个应用对象处理。建议定时器处理程序在自定进程中使用 ~~~ <?php namespace App\Init; use Swoolefy\Core\Swfy; use Swoolefy\Core\StartInit; use Swoolefy\Core\Process\ProcessManager; class Init extends StartInit { public function onInit() { // 创建一个测试自定义进程 ProcessManager::getInstance()->addProcess('test', \App\Process\TestProcess\Test::class); // 创建一个定时器处理进程 ProcessManager::getInstance()->addProcess('tick', \App\Process\TickProcess\Tick::class); } /** * onWorkerStart * @param $server * @return */ public function onWorkerStart($server,$worker_id) {} } ~~~ `App\Process\TickProcess\Tick`定义的自定义进程处理类,必须继承`Swoolefy\Core\Process\AbstractProcess` ~~~ <?php namespace App\Process\TickProcess; use Swoole\Process; use Swoolefy\Core\Swfy; use Swoolefy\Core\Application; use Swoolefy\Core\Process\AbstractProcess; use Swoolefy\Core\Process\ProcessManager; use Swoolefy\Core\Process\SwooleProcess; use Swoolefy\Core\Timer\TickManager; class Tick extends AbstractProcess { public $SwooleProcessHander; public function run(Process $process) { // 协议层配置 // $conf = Swfy::getConf(); // var_dump($conf); // 创建定时器处理实例 TickManager::getInstance()->tickTimer(3000, ['App\Process\TickProcess\TickTestController','ticktest'], ['fffff']); } public function onReceive($str, ...$args) { // 测试退出进程,退出后,底层重新拉起一个新的进程 // $process = $this->getProcess(); // $process->kill($process->pid, SIGTERM); // $process->wait(); } public function onShutDown() {} public function __get($name) { return Application::$app->$name; } } ~~~ `TickManager::getInstance()->tickTimer(3000, ['App\Process\TickProcess\TickTestController','ticktest'], '定时器测试');` 这一行就是在自定义进程中创建定时任务处理实例,`App\Process\TickProcess\TickTestController`定时任务处理类,必须继承`Swoolefy\Core\Process\ProcessController`,例如: ~~~ <?php namespace App\Process\TickProcess; use Swoolefy\Core\Application; use Swoolefy\Core\Process\ProcessController; class TickTestController extends ProcessController { public function ticktest($data) { var_dump($data); var_dump('这是自定义的定时器进程'); } } ~~~ 参数$data就是定时器执行时的传递的参数,可以是数组或者字符串,例子中`var_dump($data)`将打印出“定时器测试”