zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    spring的断言工具类Assert的基本使用
    开发工具推荐
    IDEA 接口调试插件 HTTP Client
    【笔记0-开篇】面试官系统精讲Java源码及大厂真题
    面试题:HashSet、TreeSet 和HashMap 的实现与原理
    Mybatis面试问题集锦
    分组拼接字符串,GROUP_CONCAT
    跨表更新,Mysql Update Join
    【设计模式】UML类图及Java的类之间的关系
    Sql性能优化梳理
  • 原文地址:https://www.cnblogs.com/lalalagq/p/9969009.html
Copyright © 2011-2022 走看看