1

我尝试使用 tornado.platform.twisted 来集成 txyam memcached 客户端,但是当我尝试检查它的功能时,会抛出下一个错误:

Traceback (most recent call last):
File "swcomet/tx_memcache_helper.py", line 32, in <module>
mem_helper = MemcacheHelper()
File "swcomet/tx_memcache_helper.py", line 19, in __init__
self.add(4)
File "/home/rustem/work/sw.services.swcomet.python/venv/local/lib/python2.7/site-packages/tornado/gen.py", line 117, in wrapper
gen = func(*args, **kwargs)
File "swcomet/tx_memcache_helper.py", line 25, in add
self.mem.getPickled(user_id, decompress=True)
File "/home/rustem/work/sw.services.swcomet.python/venv/lib/python2.7/site-packages/txyam/client.py", line 133, in getPickled
return self.get(key, **kwargs).addCallback(handleResult, uncompress)
File "/home/rustem/work/sw.services.swcomet.python/venv/lib/python2.7/site-packages/txyam/client.py", line 27, in wrapper
func = getattr(self.getClient(key), cmd)
File "/home/rustem/work/sw.services.swcomet.python/venv/lib/python2.7/site-packages/txyam/client.py", line 48, in getClient
raise NoServerError, "No connected servers remaining."

txyam.client.NoServerError:没有剩余的连接服务器。

转储该错误的源代码:

import tornado.ioloop
import tornado.gen
from txyam.client import YamClient
from swtools.date import _ts
import tornado.platform.twisted

MEMHOSTS = ['127.0.0.1111']
USER_EXPIRATION_TIME = 61


class MemcacheHelper(object):
   def __init__(self, *a, **kw):
     try:
        self.mem = YamClient(["127.0.0.1"])
     except Exception, e:
        print "ERror", e
     self.clients = set()
     self.add(4)

  @tornado.gen.engine
  def add(self, user_id, expire=None):
     self.clients.add(user_id)
     expire = expire or USER_EXPIRATION_TIME
     self.mem.getPickled(user_id, decompress=True)
     print "hmmm"

  if __name__ == '__main__':
    print "trying to start on top of IOLOOP"
    ioloop = tornado.ioloop.IOLoop.instance()
    #reactor = TornadoReactor(ioloop)
    mem_helper = MemcacheHelper()
    #mem_helper.add(4)
    ioloop.start()

请帮我解决这个问题!

4

1 回答 1

1

在至少建立一个连接之前,txyam 似乎不允许您执行任何 memcache 操作:

def getActiveConnections(self):
    return [factory.client for factory in self.factories if not factory.client is None]


def getClient(self, key):
    hosts = self.getActiveConnections()
    log.msg("Using %i active hosts" % len(hosts))
    if len(hosts) == 0:
        raise NoServerError, "No connected servers remaining."
    return hosts[ketama(key) % len(hosts)]

它试图立即建立这些连接:

def __init__(self, hosts):
    """                                                                                                                                                                                       
    @param hosts: A C{list} of C{tuple}s containing hosts and ports.                                                                                                                          
    """
    self.connect(hosts)

但是连接设置是异步的,它不会公开事件来指示何时至少建立了一个连接。

因此,您的代码失败,因为您add在任何连接存在之前立即调用。一个好的长期修复是针对 txyam 提交错误报告,因为这不是一个很好的界面。 YamClient可以有一个方法,当您实际被允许使用该实例时,该whenReady方法会返回Deferred触发。YamClient或者可能有一个替代构造函数返回一个Deferred与实例一起触发的YamClient,但只有在它可以使用之后。

于 2012-10-22T12:33:32.020 回答