workman 定时任务

tp6

第一步:

创建自定义指令

php think make:command Hello hello

会生成一个app\command\Hello命令行指令类,我们修改内容如下:
第二步,配置config/console.php文件
<?php
return [
    'commands' => [
        'hello' => 'app\command\Hello',
    ]
];


第三步,测试-命令帮助-命令行下运行

php think hello

安装workman
cmd到根目录下:
composer require workerman/workerman
安装workman

app\command\Hello中代码

<?php

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;

use Workerman\Lib\Timer;
use Workerman\Worker;


class Hello extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('hello');
        // 设置参数
        
    }

    protected function execute(Input $input, Output $output)
    {
        $task = new Worker();
        // 开启多少个进程运行定时任务,注意业务是否在多进程有并发问题
        $task->count = 1;
        $task->onWorkerStart = function(Worker $task)
        {
            // 每2.5秒执行一次
            $time_interval = 2.5;
            Timer::add($time_interval, function()
            {
                echo "task run\n";
            });
        };

        // 运行worker
        Worker::runAll();
    	// 指令输出
    	$output->writeln('hello');
    }
}

  

到此为止,直接在tp根目录运行,php think hello  每隔2.5秒执行一遍脚本,输出task run




terminal 运行tp方法 

php public/index.php index/index/hello

























































原文地址:https://www.cnblogs.com/yszr/p/15642231.html