python-memcached无法获取,或设置'JAC_SF'

时间:2013-11-18 17:33:00

标签: python memcached python-memcached

在检查了一大堆东西之后,我发现对于某些键,python-memcached只是无法获取,或者将它们设置在我的机器上(Mac OSX 10.9,自制内存memcache 1.4.15,pip安装了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
<德尔> 尝试在我的python程序中设置多个键时,我的结果非常不一致, 导入memcache TWO_HOURS = 2 * 60 * 60 mc_cl = memcache.Client('127.0.0.1') mapping = {...} mc_cl.flush_all() ret = mc_cl.set_multi(mapping = mapping,time = TWO_HOURS) getret = mc_cl.get_multi(mapping.keys()) 如果len(映射)!= len(getret):     为'k'打印'未设置\ n \ t {}'.format('\ n \ t \ .join([str((k,mapping [k]))                                               [a for a mapping.keys()                                                如果不是在getret.keys()]])) 在分析了原始内存缓存输出后,似乎并未设置所有密钥,并且此后并未请求所有密钥。在Mac OSX 10.9上,所有事情都在自制和点播上完成并保持最新。 下面的Memcache输出并读取它表明只有101个键被尝试写入和读取,而有228个项目。 进口重新 gamesSet = [] gamesGet = [] 使用open('memout.log')作为f:     对于f.read()中的行.split('\ n'):         match = re.match('^ 21 OK 22好的 21存储

1 个答案:

答案 0 :(得分:0)

这是一个奇怪的问题,我目前正在与python-memcached库的作者/维护者进行辩论,https://github.com/linsomniac/python-memcached/issues/21

随机地,这里的错误是第一行,

mc_cl = memcache.Client('127.0.0.1')

客户端需要一个可迭代的,但不能确保检查获取字符串,因此以非常pythonic的方式,它将此字符串视为可迭代并迭代它,设置每一个'1','2','7''。'并且'0'是memcached服务器的IP地址,但真正混淆的地方是'0'实际上是一个有效的IP地址,并且评估为0.0.0.0并由本地memcached服务器提供服务,如果是这样的话你已经设置好了(这就是我的开发环境的设置方式)。

如果您只是将其更改为,

mc_cl = memcache.Client(('127.0.0.1',))

所有内容都完全按预期工作,但如果您传入'127.0.0.1'字符串,则不会收到任何错误消息,也不会显示任何未设置密钥的报告,也无法获得“获取”,只是非常不稳定和不可解释的行为,如上所述。