在检查了一大堆东西之后,我发现对于某些键,python-memcached 只是无法获取或在我的机器上设置它们(Mac OSX 10.9,自制 memcache 1.4.15,pip installed python-memcached 1.53) . 我的第一个问题是在黑暗中拍摄,不清楚发生了什么,但经过更多挖掘后,我现在确定了。
这一切都取决于 def _get_server(self, key):
如果我们添加两个调试打印输出行,
def _get_server(self, key):
if isinstance(key, tuple):
serverhash, key = key
else:
serverhash = serverHashFunction(key)
for i in range(Client._SERVER_RETRIES):
server = self.buckets[serverhash % len(self.buckets)]
if server.connect():
#print "(using server %s)" % server,
print 'got server {} for {}'.format(serverhash % len(self.buckets),
key)
return server, key
print 'server {} failed for {}'.format(serverhash % len(self.buckets),
key)
serverhash = serverHashFunction(str(serverhash) + str(i))
return None, None
试图获取或设置密钥'NFL::CAR_TB',
import memcache
mc_cl = memcache.Client('127.0.0.1')
mc_cl.set('JAC_SF', 1)
mc_cl.get('JAC_SF')
mc_cl.set('UAC_SF', 1)
mc_cl.get('UAC_SF')
结果,在我的机器上,
server 3 (inet:.:11211 (dead until 1384821475)) failed for JAC_SF
server 7 (inet:.:11211 (dead until 1384821475)) failed for JAC_SF
server 3 (inet:.:11211 (dead until 1384821475)) failed for JAC_SF
server 2 (inet:7:11211 (dead until 1384821475)) failed for JAC_SF
server 5 (inet:.:11211 (dead until 1384821475)) failed for JAC_SF
server 7 (inet:.:11211 (dead until 1384821475)) failed for JAC_SF
server 3 (inet:.:11211 (dead until 1384821475)) failed for JAC_SF
server 7 (inet:.:11211 (dead until 1384821475)) failed for JAC_SF
server 7 (inet:.:11211 (dead until 1384821475)) failed for JAC_SF
server 2 (inet:7:11211 (dead until 1384821475)) failed for JAC_SF
server 3 (inet:.:11211 (dead until 1384821475)) failed for JAC_SF
server 7 (inet:.:11211 (dead until 1384821475)) failed for JAC_SF
server 3 (inet:.:11211 (dead until 1384821475)) failed for JAC_SF
server 2 (inet:7:11211 (dead until 1384821475)) failed for JAC_SF
server 5 (inet:.:11211 (dead until 1384821475)) failed for JAC_SF
server 7 (inet:.:11211 (dead until 1384821475)) failed for JAC_SF
server 3 (inet:.:11211 (dead until 1384821475)) failed for JAC_SF
server 7 (inet:.:11211 (dead until 1384821475)) failed for JAC_SF
server 7 (inet:.:11211 (dead until 1384821475)) failed for JAC_SF
server 2 (inet:7:11211 (dead until 1384821475)) failed for JAC_SF
server 7 (inet:.:11211 (dead until 1384821475)) failed for UAC_SF
got server 6 for UAC_SF
server 7 (inet:.:11211 (dead until 1384821475)) failed for UAC_SF
got server 6 for UAC_SF
如果我将 _get_server 更改为
def _get_server(self, key):
choices = range(len(self.buckets) - 1)
random.shuffle(choices)
if isinstance(key, tuple):
choice, key = key
else:
choice = choices.pop()
for _ in range(Client._SERVER_RETRIES):
server = self.buckets[choice]
if server.connect():
#print "(using server %s)" % server,
print 'got server {} for {}'.format(choice,
key)
return server, key
print 'server {} ({}) failed for {}'.format(choice, server, key)
choice = choices.pop()
return None, None
然后一切都会好很多,
got server 4 for JAC_SF
server 0 (inet:1:11211 (dead until 1384821552)) failed for JAC_SF
server 3 (inet:.:11211 (dead until 1384821552)) failed for JAC_SF
got server 4 for JAC_SF
server 7 (inet:.:11211 (dead until 1384821552)) failed for UAC_SF
server 0 (inet:1:11211 (dead until 1384821552)) failed for UAC_SF
got server 6 for UAC_SF
server 7 (inet:.:11211 (dead until 1384821552)) failed for UAC_SF
server 2 (inet:7:11211 (dead until 1384821552)) failed for UAC_SF
server 5 (inet:.:11211 (dead until 1384821552)) failed for UAC_SF
server 1 (inet:2:11211 (dead until 1384821552)) failed for UAC_SF
got server 6 for UAC_SF