Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Python对Redis进行实时用户数统计

Python对Redis进行实时用户数统计安装redis
#wget http://redis.googlecode.com/files/redis-2.6.14.tar.gz
#tar xzf redis-2.6.14.tar.gz
#cd redis-2.6.14
#make执行make的时候报错,具体报错信息如下:
zmalloc.o: In function `zmalloc_used_memory":
/usr/local/redis-2.6.14/src/zmalloc.c:223: undefined reference to `__sync_add_and_fetch_4"
collect2: ld returned 1 exit status
make[1]: *** [redis-server] Error 1
make[1]: Leaving directory `/usr/local/redis-2.6.14/src在make操作的时候加一个执行参数:makeCFLAGS="-march=i686"
然后就不报错了。
编译的可执行文件在src目录中,启动运行Redis:
# src/redis-server如图正确启动后,安装redis-py
git clone https://github.com/andymccurdy/redis-py.git
cd redis-py
python setup.py installpython操作在线用户数的简单代码:
#!/usr/bin/python
import redis
r=redis.StrictRedis(host="192.168.39.138",port=6379)
r.set("online","0")
r.incr("online")
r.incr("online")
r.incr("online")
r.decr("online")
print r.get("online")如果有新的用户登录incr插入一条记录,如果有用户下线可以使用decr或者规定一个超时时间,对不活跃的用户自动删除记录。
#./redis.py
2模式测试,当前有三个用户登录变为在线,同时一个用户离线,所以显示在线用户为2人,实时性更高。推荐阅读:Python脚本获取Linux系统信息 http://www.linuxidc.com/Linux/2013-08/88531.htmPython文件处理:读取文件 http://www.linuxidc.com/Linux/2013-08/88496.htm如何发布自定义的Python模块 http://www.linuxidc.com/Linux/2013-08/88495.htmPython爬虫多线程抓取代理服务器 http://www.linuxidc.com/Linux/2013-07/87289.htmPython中re(正则表达式)模块详解 http://www.linuxidc.com/Linux/2013-08/88588.htmRedis集群明细文档 http://www.linuxidc.com/Linux/2013-09/90118.htmUbuntu 12.10下安装Redis(图文详解)+ Jedis连接Redis http://www.linuxidc.com/Linux/2013-06/85816.htmRedis系列-安装部署维护篇 http://www.linuxidc.com/Linux/2012-12/75627.htmCentOS 6.3安装Redis http://www.linuxidc.com/Linux/2012-12/75314.htmRedis 的详细介绍:请点这里
Redis 的下载地址:请点这里Python 的详细介绍:请点这里
Python 的下载地址:请点这里