如何在龙卷风IOLoop上插入txyam

时间:2012-10-22 09:22:59

标签: memcached twisted tornado

我尝试使用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()

请帮我解决这个问题!

1 个答案:

答案 0 :(得分:1)

在至少建立一个连接之前,

txyam似乎不允许您执行任何内存缓存操作:

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与{{1}}实例一起触发,但只有在可以使用它之后才会触发。

相关问题