• 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

























































  • 相关阅读:
    全选、全不选、反选
    IE Tester 怎样使用firebug 调试工具?
    策略模式
    模板方法模式
    迭代器模式——android中使用
    android 实现圆形波纹动画
    android 窗口的使用
    AlertDialog 基本使用
    国外android 网站
    ViewDragHelper 任意拖动
  • 原文地址:https://www.cnblogs.com/yszr/p/15642231.html
Copyright © 2020-2023  润新知