• swoole异步任务数据报表生成


    <?php
    
    include 'vendor/autoload.php';
    
    class server
    {
        private $serv;
        private $db;
    
        /**
         * [__construct description]
         * 构造方法中,初始化 $serv 服务
         */
        public function __construct()
        {
            $this->serv = new swoole_server('0.0.0.0', 9501);
            //初始化swoole服务
            $this->serv->set(array(
                'worker_num' => 1,
                'daemonize' => false, //是否作为守护进程,此配置一般配合log_file使用
                'max_request' => 1000,
                'log_file' => './swoole.log',
                'task_worker_num' => 2
            ));
    
            //设置监听
            $this->serv->on('Start', array($this, 'onStart'));
            $this->serv->on('WorkerStart', array($this, 'onWorkerStart'));
            $this->serv->on('Connect', array($this, 'onConnect'));
            $this->serv->on("Receive", array($this, 'onReceive'));
            $this->serv->on("Close", array($this, 'onClose'));
            $this->serv->on("Task", array($this, 'onTask'));
            $this->serv->on("Finish", array($this, 'onFinish'));
    
            //开启
            $this->serv->start();
        }
    
        /**
         * 连接MySQL
         */
        public function connect()
        {
            $this->db = new medoo([
                'database_type' => 'mysql',
                'database_name' => 'xxxx',
                'server' => '127.0.0.1',
                'username' => 'root',
                'password' => 'xxxxx',
                'prefix' => '',
                'charset' => 'utf8'
            ]);
        }
    
        /**
         * 在worker启动的时候,创建MySQL连接池
         * @param $ser
         * @param $worker_id
         */
        public function onWorkerStart($ser, $worker_id)
        {
            if ($ser->taskworker) {
                $this->connect();
                echo "taskWorker Starting ." . spl_object_hash($this->db) . ".........
    ";
            } else {
                echo 'wokerStart...db has=>' . PHP_EOL;
            }
        }
    
        public function onStart($serv)
        {
            echo SWOOLE_VERSION . " onStart
    ";
        }
    
        public function onConnect($serv, $fd)
        {
            echo $fd . "Client Connect.
    ";
        }
    
        /**
         * 这个发生在worker进程中,当接受到来自mater进程中reactor线程转发过来的客户端
         * @param $serv
         * @param $fd
         * @param $from_id
         * @param $data
         */
        public function onReceive($serv, $fd, $from_id, $data)
        {
            echo "Get Message From Client {$fd}:{$data}
    ";
            $param = array(
                'fd' => $fd
            );
            $serv->task(json_encode($param));
            echo "Continue Handle Worker
    ";
        }
    
        public function onClose($serv, $fd)
        {
            echo "Client Close.
    ";
        }
    
        /**
         * 异步任务,用来生成csv表格数据导出
         * @param $serv
         * @param $task_id
         * @param $from_id
         * @param $data
         * @return string
         */
        public function onTask($serv, $task_id, $from_id, $data)
        {
            echo "This Task {$task_id} from Worker {$from_id}
    ";
            for ($i = 0; $i < 2; $i++) {
                $all_pager = $this->db->count('dtb_checking');
                echo '总条数:' . $all_pager . PHP_EOL;
                // 其实这里需要判断是否是连接超时,等一系列操作,但是这里就是为了测试数据库连接失效情况。
                if ($all_pager === false) {
                    echo '重新连' . PHP_EOL;
                    $this->connect();
                    continue;
                }
                $fp = fopen(__DIR__ . DIRECTORY_SEPARATOR . rand() . '.csv', 'w');
                $page_size = 500;
                $page = ceil($all_pager / $page_size);
                for ($p = 1; $p <= $page; $p++) {
                    echo '第' . $p . '页' . PHP_EOL;
                    $start = ($p - 1) * $page_size;
                    $list = $this->db->select('dtb_checking', ['id', 'real_name', 'account_no'], ['LIMIT' => [$start, 500]]);
                    foreach ($list as $k => $row) {
                        $temp = [
                            iconv('utf-8', 'gbk', $row['id']),
                            iconv('utf-8', 'gbk', $row['real_name']),
                            iconv('utf-8', 'gbk', $row['account_no']),
                        ];
                        fputcsv($fp, $temp);
                    }
                }
                break;
            }
            return "Task {$task_id}'s result";
        }
    
        public function onFinish($serv, $task_id, $data)
        {
            echo "Task {$task_id} finish
    ";
            echo "Result: {$data}
    ";
        }
    }
    
    $server = new server();
  • 相关阅读:
    SICP 习题1.16-1.19体会
    设计模式C++实现——组合模式
    QT官方下载地址
    浅析三角形測试程序
    操作系统开发之——进入保护模式
    PowerDesigner 打印错误
    @RequestParam注解一般用法
    Eclipse配置Maven的一些问题
    在windows上部署使用redis出现的问题及解决方法
    Eclipse导入模板格式Xml配置文件
  • 原文地址:https://www.cnblogs.com/shiwenhu/p/6422670.html
Copyright © 2020-2023  润新知