• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php–swoole之简单定时器使用

php 搞代码 4年前 (2022-01-22) 45次浏览 已收录 0个评论

这篇文章主要介绍了关于php–swoole之简单定时器使用 ,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

创建文件 Sw.php

<?php /*** 单例操作数据库 写入*/class Sw{    private $_db = null;    private static $_instance;      //静态配置可拉出  设置成静态文件  或者常量    public function __construct(){        $this->_db =  mysql_connect('192.168.2.130', 'root', '123456');//26上面数据库配置    }	public static function getInstance()    {            if(! (self::$_instance instanceof self) ) {                self::$_instance = new self();            }            return self::$_instance;        }      //下列函数用于调整对象的克隆行为   private function __clone() {}    public function insert_one(){    	if(!mysql_select_db('sakila')){            return false;        }        $time = time();    	$sql = "INSERT INTO sw_db (add_time) VALUES (".$time.")";         $result = mysql_query($sql,$this->_db);    }}

创建文件 Timer.php

 <?phpdate_default_timezone_set('Asia/Shanghai');/*** swoole 定时器类*/require_once 'Sw.php';class Timer{	private static $_sw = null;	public function __construct() {		if(!isset($this->_sw)){			$this->_sw =  new swoole_server('192.168.2.130', 9501);		}	}	public function run(){        $this->_sw->on('WorkerStart', array($this, 'onStart'));        $this->_sw->on('Receive', array($this, 'onReceive'));        $this->_sw->start();	}	public function onStart(swoole_server $_sw){	    $_sw->tick(1000, function ($id) {	    	$this->upsert();	       	});	}	private function upsert(){		$db_sw = Sw::getInstance();		$db_sw->insert_one();	}	public function onReceive(swoole_server $_sw){		echo '1';	}}$timer = new Timer();$timer->run();

执行方式: 放入你的linux系统中

我的存放目录是根目录下:ydy/swoole

编辑shell文件,创建fsw.sh

 #!/bin/shpid=`ps -ef | grep '/ydy/swoole/Timer.php' | grep -v grep |awk '{print $2}'`if [ "${pid}" = "" ]	then	php /ydy/swoole/Timer.php &fi

并结合linux crontab监听脚本。

以上就是简易的p本&文来源gao@daima#com搞(%代@#码网@hp和swoole没秒写入数据库例子。

相关推荐:

如何用swoole与websocket开发一个聊天室

php加swoole加mysql 仿webqq及时聊天

利用php +swoole如何实现异步任务队列

以上就是php–swoole之简单定时器使用 的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php–swoole之简单定时器使用

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址