1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
[root@fast-node-
652
ceph]# python ../pkg/redisbase.py
Traceback (most recent call last):
File
"../pkg/redisbase.py"
, line
85
,
in
redis_hget
values = self.pipe.execute()
File
"/usr/lib/python2.7/site-packages/redis/client.py"
, line
2578
,
in
execute
return
execute(conn, stack, raise_on_error)
File
"/usr/lib/python2.7/site-packages/redis/client.py"
, line
2470
,
in
_execute_transaction
response = self.parse_response(connection,
'_'
)
File
"/usr/lib/python2.7/site-packages/redis/client.py"
, line
2536
,
in
parse_response
self, connection, command_name, **options)
File
"/usr/lib/python2.7/site-packages/redis/client.py"
, line
577
,
in
parse_response
response = connection.read_response()
File
"/usr/lib/python2.7/site-packages/redis/connection.py"
, line
574
,
in
read_response
raise response
ResponseError: handle request, command
'EXEC'
is
not allowed
m6
[root@fast-node-
652
ceph]# python ../pkg/redisbase.py
Traceback (most recent call last):
File
"../pkg/redisbase.py"
, line
85
,
in
redis_hget
values = self.pipe.execute()
File
"/usr/lib/python2.7/site-packages/redis/client.py"
, line
2578
,
in
execute
return
execute(conn, stack, raise_on_error)
File
"/usr/lib/python2.7/site-packages/redis/client.py"
, line
2470
,
in
_execute_transaction
response = self.parse_response(connection,
'_'
)
File
"/usr/lib/python2.7/site-packages/redis/client.py"
, line
2536
,
in
parse_response
self, connection, command_name, **options)
File
"/usr/lib/python2.7/site-packages/redis/client.py"
, line
577
,
in
parse_response
response = connection.read_response()
File
"/usr/lib/python2.7/site-packages/redis/connection.py"
, line
574
,
in
read_response
raise response
ResponseError: handle request, command
'EXEC'
is
not allowed
|
解决办法:
1
|
self.pipe = self.conn.pipeline(transaction=False)
|
整行代码上贴
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
#!/usr/bin/env python
#-*-coding:UTF-8-*-
"""
@Item : cheetah v1.0
@Author : william
@Group : XXX XXX
@Date : 2017-03-15
@E-mail :
@Funtion:
redis_set : Redis in the form of pipe insert data, json hash as a string print
redis_get : Redis inprintion, and json string into the original print
"""
import
sys,os,time,redis,traceback,json
sys.path.append(
"/data/cheetah/"
)
reload
(sys)
sys.setdefaultencoding(
"utf8"
)
from
pkg
import
log
from
pkg
import
config
LOG
=
log.get_logger(__name__)
cfg
=
config
RedisCONF
=
cfg.load_cfg()[
"redis"
]
class
RedisBase(
object
):
''' Establish redis session connection pool '''
def
__init__ (
self
):
self
.host
=
RedisCONF[
'host'
]
self
.port
=
RedisCONF[
'port'
]
self
.db
=
RedisCONF[
'db'
]
try
:
pool
=
redis.ConnectionPool(host
=
self
.host, port
=
self
.port, db
=
self
.db)
self
.conn
=
redis.Redis(connection_pool
=
pool)
self
.pipe
=
self
.conn.pipeline(transaction
=
False
)
except
:
LOG.error(traceback.print_exc())
return
traceback.print_exc()
def
redis_set(
self
,keys
=
None
,values
=
None
):
''' Insert redis databases,keys = key ,values = value'''
try
:
self
.pipe.
set
(keys,json.dumps(values))
self
.pipe.execute()
except
:
LOG.error(traceback.print_exc())
return
traceback.print_exc()
#add by william, for hash set
def
redis_hset(
self
,field, key,values ):
'''insert redis key, field ,values'''
try
:
self
.pipe.hset(field,key, json.dumps(values))
self
.pipe.execute()
except
:
LOG.error(traceback.print_exc())
return
traceback.print_exc()
#add by william, for hash set and add expire
def
redis_set_expire(
self
, key,values ):
'''insert redis key, field ,values'''
try
:
self
.pipe.
set
(key,json.dumps(values))
self
.pipe.expire(key,
604800
)
self
.pipe.execute()
except
:
LOG.error(traceback.print_exc())
return
traceback.print_exc()
#add by william, get from hash
def
redis_hget(
self
, key, field
=
None
):
'''get by key, return the dict'''
try
:
fields
=
[]
if
field:
fields.append(field)
else
:
fields
=
self
.conn.hkeys(key)
for
f
in
fields:
self
.pipe.hget(key, f)
values
=
self
.pipe.execute()
values
=
[json.loads(i)
for
i
in
values]
return
dict
(
zip
(fields, values))
except
:
print
traceback.format_exc()
return
key
def
redis_get(
self
,argv):
''' Getting single KYES values ,argv : is keys'''
try
:
self
.pipe.get(argv)
return
json.loads(
self
.pipe.execute()[
0
])
except
:
LOG.error(traceback.print_exc())
return
traceback.print_exc()
def
redis_getkeys(
self
):
''' Getting all keys '''
try
:
self
.pipe.keys(
'*'
)
return
self
.pipe.execute()[
0
]
except
:
LOG.error(traceback.print_exc())
return
traceback.print_exc()
def
redis_delete(
self
,keys):
try
:
self
.pipe.delete(keys)
self
.pipe.execute()
return
'Delete True'
except
:
LOG.error(traceback.print_exc())
return
'Delete False'
if
__name__
=
=
"__main__"
:
sc
=
RedisBase()
bs
=
sc.redis_hget(
'm6'
)
for
x,y
in
bs.items():
for
a,b
in
y.items():
if
a
=
=
'ceph_df'
:
print
b
|
本文转自 swq499809608 51CTO博客,原文链接:http://blog.51cto.com/swq499809608/1934394