在检查了一大堆东西后,我发现对于一些密钥,python memcached无法获得,或者无法在我的机器上设置它们(Mac OSX 10.9,自制程序memcached 1.4.15,pip安装的python memcached1.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更改为(_G)
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
这是我目前正在与python memcached库的编写者/维护者讨论的一个奇怪的问题,https://github.com/linsomniac/python-memcached/issues/21.
随机地,这里出了问题的是第一行,
mc_cl = memcache.Client('127.0.0.1')
客户端需要一个可迭代的,但不确保检查它没有获取字符串,因此以一种非常Python的方式,它将此字符串视为可迭代的并对其进行迭代,设置"1"、"2"one_answers"7"中的每一个和"0"是memcached服务器的IP地址,但真正的困惑开始于"0"实际上是一个有效的IP地址、计算值为0.0.0.0并且由本地memcached服务,如果这就是你的设置方式(我的开发环境就是这样设置的)。
如果你简单地将其更改为
mc_cl = memcache.Client(('127.0.0.1',))
一切都如预期的那样工作,但如果你输入"127.0.0.1"字符串,你不会收到任何错误消息,也不会收到任何未设置密钥的报告,也不会被"获取",只是像上面这样非常不稳定且无法解释的行为。