python连接memcached

简介:

1.下载安装

1
2
3
4
5
wget http: //www .memcached.org /files/memcached-1 .5.3. tar .gz
tar  -zxvf memcached-1.5.3. tar .gz
cd  memcached-1.5.3
. /configure  --prefix= /data/program/memcached
make  &&  make  install



2.启动memcache

1
. /memcached  -d -u root -l 127.0.0.1 -m 50 -p 11211 -c 512 -P  /data/program/memcached/logs/memcached .pid

参数:

-u 运行身份

-p 指定端口号(默认11211)  

-m 指定最大使用内存大小(默认64MB)  

-t 线程数(默认4)  

-l 连接的IP地址, 默认是本机  

-d 以后台守护进程的方式启动

-c 最大同时连接数,默认是1024

-P 制定memecache的pid文件

-h 打印帮助信息


3.连接测试

1
2
3
4
5
6
7
[root@huangzp3 bin] # telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is  '^]' .
add  test  1 0 5
hello
STORED



4.memcache集群的操作

4.1下载api模块python-memcached

1
wget https: //pypi .python.org /packages/f7/62/14b2448cfb04427366f24104c9da97cf8ea380d7258a3233f066a951a8d8/python-memcached-1 .58. tar .gz

linux环境:

1
2
3
tar  python-memcached-1.58. tar .gz
cd  python-memcached-1.58
python setup.py  install


参考链接:https://www.cnblogs.com/xiao1/p/5858967.html


4.2启动

1
2
3
. /memcached  -d -u root -p 11211
. /memcached  -d -u root -p 11212
. /memcached  -d -u root -p 11213

例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/env python
import  memcache
 
# 初始化一个memcache客户端对象,端口权重分别为1:2:1,开启错误debug显示
mc  =  memcache.Client([( "192.168.2.230:11211" , 1 ),( "192.168.2.230:11212" , 2 ),( "192.168.2.230:11213" , 1 )],debug = True )
mc. set ( "name" , "huangzhenping" )
print (mc.get( "name" ))
 
mc1  =  memcache.Client([ "192.168.2.230:11211" ],debug = True )
print (mc1.get( "name" ))
 
mc2  =  memcache.Client([ "192.168.2.230:11212" ],debug = True )
print (mc1.get( "name" ))
 
mc3  =  memcache.Client([ "192.168.2.230:11213" ],debug = True )
print (mc3.get( "name" ))


运行结果:

huangzhenping

None

None

huangzhenping











本文转自 huangzp168 51CTO博客,原文链接:http://blog.51cto.com/huangzp/2045119,如需转载请自行联系原作者
目录
相关文章
|
20天前
|
NoSQL Unix 网络安全
【Azure Cache for Redis】Python Django-Redis连接Azure Redis服务遇上(104, 'Connection reset by peer')
【Azure Cache for Redis】Python Django-Redis连接Azure Redis服务遇上(104, 'Connection reset by peer')
【Azure Cache for Redis】Python Django-Redis连接Azure Redis服务遇上(104, 'Connection reset by peer')
|
1月前
|
机器学习/深度学习 数据可视化 Python
如何可视化神经网络的神经元节点之间的连接?附有Python预处理代码
该博客展示了如何通过Python预处理神经网络权重矩阵并将其导出为表格,然后使用Chiplot网站来可视化神经网络的神经元节点之间的连接。
33 0
如何可视化神经网络的神经元节点之间的连接?附有Python预处理代码
|
1月前
|
数据可视化 搜索推荐 数据挖掘
基于Python flask 的数据可视化平台,可定制,可连接数据库
本文介绍了一个基于Python Flask框架开发的可定制数据可视化平台,该平台支持多种数据库连接,并提供丰富的图表类型和个性化设置,以实现交互式数据分析和展示。
基于Python flask 的数据可视化平台,可定制,可连接数据库
|
20天前
|
缓存 NoSQL 网络安全
【Azure Redis 缓存】 Python连接Azure Redis, 使用redis.ConnectionPool 出现 "ConnectionResetError: [Errno 104] Connection reset by peer"
【Azure Redis 缓存】 Python连接Azure Redis, 使用redis.ConnectionPool 出现 "ConnectionResetError: [Errno 104] Connection reset by peer"
|
21天前
|
存储 API 开发工具
【Azure Developer】使用 Python SDK连接Azure Storage Account, 计算Blob大小代码示例
【Azure Developer】使用 Python SDK连接Azure Storage Account, 计算Blob大小代码示例
|
24天前
|
网络协议 Python
python requests库如何使用http连接池降低延迟 keepalive复用连接
Python的`requests`库通过内置的连接池机制支持HTTP Keep-Alive特性,允许复用TCP连接以发送多个请求,减少连接开销。默认情况下,`requests`不显式禁用Keep-Alive,其行为取决于底层HTTP库(如urllib3)及服务器的支持。通过创建`Session`对象并自定义`HTTPAdapter`,可以调整连接池大小和重试策略,进一步优化连接复用。测试显示,使用`Session`和定制的`HTTPAdapter`比普通请求方法能显著减少连续请求间的时间消耗,体现了Keep-Alive的优势。
|
2月前
|
存储 Java 网络安全
如何使用Python批量连接网络设备?
【7月更文挑战第4天】
43 1
如何使用Python批量连接网络设备?
|
30天前
|
API 数据库 开发者
Python连接Neo4j工具比较 Neo4j Driver、py2neo
Python连接Neo4j工具比较 Neo4j Driver、py2neo
20 0
|
3月前
|
Python
python之字符串定义、切片、连接、重复、遍历、字符串方法
python之字符串定义、切片、连接、重复、遍历、字符串方法
python之字符串定义、切片、连接、重复、遍历、字符串方法
|
3月前
|
SQL 关系型数据库 数据库连接
Python连接线上数据库的实战指南
Python连接线上数据库的实战指南
89 1