这篇文章主要介绍了关于php--swoole之简单定时器使用 ,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
创建文件 sw.php
_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
_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
立即学习“PHP免费学习笔记(深入)”;
#!/bin/sh
pid=`ps -ef | grep '/ydy/swoole/Timer.php' | grep -v grep |awk '{print $2}'`
if [ "${pid}" = "" ]
then
php /ydy/swoole/Timer.php &
fi并结合linux crontab监听脚本。
CRMEB打通版是一款全开源支持免费商用的PHP 多语言商城系统;CRMEB技术团队历经6年时间匠心之作!系统采用前后端分离技术,基于TP6+Uni-app框架开发;客户移动端采用uni-app开发,管理后台前端使用iviewUI开发。系统支持微信公众号端、微信小程序端、H5端、PC端多端账号同步,可快速打包生成APP;赋能开发者,减少重复造轮子;系统支持自动检查安装环境一键安装部署,使用简单方便
以上就是简易的php和swoole没秒写入数据库例子。
相关推荐:










