<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>

                # 高階篇一 TP5命令行之守護任務源碼 > 本方法使用了多線程 執行時候使用了子線程 此方法使用的實例參見 高階篇二 使用Redis隊列發送微信模版消息 http://www.hmoore.net/mikkle/thinkphp5_study/396284 ~~~ <?php namespace app\base\command; use app\base\controller\Redis; use think\Config; use think\console\Command; use think\console\Input; use think\console\Output; use think\Exception; use think\Log; /** * Created by PhpStorm. * Power by Mikkle * QQ:776329498 * Date: 2017/6/12 * Time: 15:07 */ class Mikkle extends Command { protected $sleep = 3; protected $redis; protected $listName; protected $pcntl; public function __construct($name= null) { parent::__construct($name); $this->redis=Redis::instance(Config::get("command.redis")); $this->listName = "worker_list"; $this->pcntl =true; } protected function configure() { $this->setName('mikkle')->setDescription('Here is the mikkle\'s command '); } protected function execute(Input $input, Output $output) { while(true){ //標記后端服務運行中 $this->signWorking(); echo "==================================================".PHP_EOL; $this->autoClass(); echo "==================================================".PHP_EOL; $this->sleep(); } } /** * 自動執行 * Power: Mikkle * Email:776329498@qq.com * @return bool */ protected function autoClass() { $works = $this->getWorks(); if ($works) { foreach ($works as $item => $work) { if ($this->pcntl) { $this->pcntlWorker($work, $item); } else { $this->runWorker($work, $item); } } } else { return false; } } public function getWorks(){ try{ return $this->redis->hget($this->listName); }catch (Exception $e){ return false; } } /** * 檢測執行方法是否存在 * Power: Mikkle * Email:776329498@qq.com * @param $work * @param $item * @return bool */ protected function checkWorkerExists($work,$item){ if (class_exists($work)){ if(method_exists($work,'run')){ return true; }else{ return false; } } } /** * 運行任務 * Power: Mikkle * Email:776329498@qq.com * @param $work * @param $item */ protected function runWorker($work,$item){ try{ if($this->checkWorkerExists($work, $item)) { echo "執行[{$work}]任務" . PHP_EOL; $work::run(); Log::notice("執行[{$work}]任務"); }else{ echo "執行[{$work}]任務的run方法不存在".PHP_EOL; $this->redis->hdel($this->listName,$item); } }catch (Exception $e){ echo "執行[{$work}]任務失敗" . PHP_EOL; Log::notice($e->getMessage()); if ($this->pcntl) { $this->pcntlKill(); } } } /** * 分進程 * Power: Mikkle * Email:776329498@qq.com * @param $work * @param $item */ protected function pcntlWorker($work,$item) { try{ // 通過pcntl得到一個子進程的PID $pid = pcntl_fork(); if ($pid == -1) { // 錯誤處理:創建子進程失敗時返回-1. die ('could not fork'); } else if ($pid) { // 父進程邏輯 // 等待子進程中斷,防止子進程成為僵尸進程。 // WNOHANG為非阻塞進程,具體請查閱pcntl_wait PHP官方文檔 pcntl_wait($status, WNOHANG); } else { // 子進程邏輯 $pid_2 = pcntl_fork(); if ($pid_2 == -1) { // 錯誤處理:創建子進程失敗時返回-1. die ('could not fork'); } else if ($pid_2) { // 父進程邏輯 echo "父進程邏輯開始" . PHP_EOL; // 等待子進程中斷,防止子進程成為僵尸進程。 // WNOHANG為非阻塞進程,具體請查閱pcntl_wait PHP官方文檔 pcntl_wait($status, WNOHANG); echo "父進程邏輯結束" . PHP_EOL; } else { // 子進程邏輯 echo "子進程邏輯開始" . PHP_EOL; $this->runWorker($work,$item); echo "子進程邏輯結束" . PHP_EOL; $this->pcntlKill(); } $this->pcntlKill(); } }catch (Exception $e){ Log::error($e->getMessage()); } } /** * Kill子進程 * Power: Mikkle * Email:776329498@qq.com */ protected function pcntlKill(){ // 為避免僵尸進程,當子進程結束后,手動殺死進程 if (function_exists("posix_kill")) { posix_kill(getmypid(), SIGTERM); } else { system('kill -9' . getmypid()); } exit (); } public function signWorking(){ $this->redis->set("command","true"); $this->redis->setExpire("command",10); } public function sleep($second=""){ $second = $second ? $second : $this->sleep; // echo "開始睡眠{$second}秒!當前時間:" . date('h:i:s') . PHP_EOL; sleep(sleep($second)); //TP5的命令行 sleep($second) 不生效 echo "睡眠{$second}秒成功!當前時間:" . date('h:i:s') . PHP_EOL; } /** * @return int */ public function getSleep() { return $this->sleep; } /** * @param int $sleep * @return void */ public function setSleep($sleep) { $this->sleep = $sleep; } } ~~~ 本源碼應用到以下基礎內容 TP5實戰源碼 — 命令行 http://www.hmoore.net/mikkle/thinkphp5_study/376459 TP5實戰源碼 — 通過shell建立PHP守護程序 http://www.hmoore.net/mikkle/thinkphp5_study/376460 感謝大家關注 交流請加QQ群 321449759 ![](https://box.kancloud.cn/3499008a08e64306c68873288092a057_286x340.png)
                  <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>

                              哎呀哎呀视频在线观看