<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                在日常項目開發中定時任務是很常見的,我們在開發電商業務中搶購秒殺,拍賣等功能模塊,例如:拍下訂單后在30分鐘后自動取消未付款的訂單同時把庫存加回去。 通常我們會使用 crontab,但是 crontab 的精度不夠,最小單位是每分鐘。Swoole 就為我提供高精度毫秒的定時器。 PHP 本身的 pcntl_alarm 雖然也提供了類似的實現,但是 pcntl_alarm 最小單位只是秒,而 Swoole 是提供毫秒。而且 pcntl_alarm 不支持同時設定多個定時器程序,并且不支持異步IO。 接下來我們演示使用 ThinkPHP5 和 Swoole 的 swoole_timer_tick 函數來演示毫秒定時器。 創建 ThinkPHP5 自定義命令行 1.創建命令行類 - 創建application/console/Timer.php文件 ~~~ <?php namespace app\Console; use think\console\Command; use think\console\Input; use think\console\Output; class Timer extends Command { protected $server; // 命令行配置函數 protected function configure() { // setName 設置命令行名稱 // setDescription 設置命令行描述 $this->setName('timer:start')->setDescription('Start TCP(Timer) Server!'); } // 設置命令返回信息 protected function execute(Input $input, Output $output) { $this->server = new \swoole_server('0.0.0.0', 9501); $this->server->set([ 'worker_num' => 4, 'daemonize' => false, ]); $this->server->on('Start', [$this, 'onStart']); $this->server->on('WorkerStart', [$this, 'onWorkerStart']); $this->server->on('Connect', [$this, 'onConnect']); $this->server->on('Receive', [$this, 'onReceive']); $this->server->on('Close', [$this, 'onClose']); $this->server->start(); // $output->writeln("TCP: Start.\n"); } // 主進程啟動時回調函數 public function onStart(\swoole_server $server) { echo "Start" . PHP_EOL; } // Worker 進程啟動時回調函數 public function onWorkerStart(\swoole_server $server, $worker_id) { // 僅在第一個 Worker 進程啟動時啟動 Timer 定時器 if ($worker_id == 0) { // 啟動 Timer,每 1000 毫秒回調一次 onTick 函數, swoole_timer_tick(1000, [$this, 'onTick']); } } // 定時任務函數 public function onTick($timer_id, $params = null) { echo 'Hello' . PHP_EOL; } // 建立連接時回調函數 public function onConnect(\swoole_server $server, $fd, $from_id) { echo "Connect" . PHP_EOL; } // 收到信息時回調函數 public function onReceive(\swoole_server $server, $fd, $from_id, $data) { echo "message: {$data} form Client: {$fd}" . PHP_EOL; // 將受到的客戶端消息再返回給客戶端 $server->send($fd, "Message form Server: ".$data); } // 關閉連時回調函數 public function onClose(\swoole_server $server, $fd, $from_id) { echo "Close" . PHP_EOL; } } ~~~ 2.修改配置文件 - 文件所在 application/command.php ~~~ <?php return [ 'app\console\Timer', ]; ~~~ 接下來就可以通過命令行來啟動毫秒定時器 $ > `php think timer:start` 我們會看到以下返回結果 ![](https://box.kancloud.cn/457a2b05f65f0b692b2828b81c2a12f6_714x144.png) 當程序運行后,首次回調 onStart 函數輸出 Start,之后每 1000毫秒(每秒),輸出一次Hello,就完成了我們的毫秒定時器
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看