swoole 清除定时器提示no timer
生活随笔
收集整理的這篇文章主要介紹了
swoole 清除定时器提示no timer
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
首頁確定一個核心概念
clearTimer僅可清除當前進程的定時器server代碼如下:
<?php class Server {private $serv;private $timer;public function __construct(){$this->serv = new swoole_server("0.0.0.0", 9501);$this->serv->set(['worker_num' => 8,'daemonize' => false,]);$this->serv->on('Start', [$this, 'onStart']);$this->serv->on('Connect', [$this, 'onConnect']);$this->serv->on('Receive', [$this, 'onReceive']);$this->serv->on('Close', [$this, 'onClose']);$this->serv->start();}public function onStart($serv){$this->echoStr("Server Starting");$this->timer = $serv->tick(1000, function(){$this->echoStr("timer waiting");});// $this->timer = swoole_timer_tick(1000, function() {// });}public function onConnect($serv, $fd, $from_id){// swoole_timer_clear($this->timer);$serv->clearTimer($this->timer);$this->echoStr("Connecting! Clear Timer!");// $serv->send($fd, "Hello {$fd}!");}public function onReceive(swoole_server $serv, $fd, $from_id, $data){$this->echoStr("Get Message From Client {$fd}:{$data}");$serv->send($fd, $data);}public function onClose($serv, $fd, $from_id){$this->echoStr("Client {$fd} close connection");}public function echoStr($msg){echo '[' . date('Y-m-d H:i:s') . ']: ' . $msg . PHP_EOL;} } // 啟動服務器 Start the server $server = new Server();本意圖實現server啟動后循環輸出“timer waiting”,client連接后清除定時器的效果,然而onStart事件是在Master進程的主線程中被調用,而onConnect事件是在work進程中被回調,這里不屬于同一進程,故client連接后會提示:
PHP Warning: SwooleServer::clearTimer(): no timer...原文地址:https://segmentfault.com/a/1190000016558928
轉載于:https://www.cnblogs.com/lalalagq/p/9969009.html
總結
以上是生活随笔為你收集整理的swoole 清除定时器提示no timer的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 偶尔用得上的MySQL操作
- 下一篇: 深入java并发包源码(三)AQS独占方