2012-10-22 40 views
1

我已經嘗試過使用tornado.platform.twisted整合txyam memcached的客戶端,但是當我試圖檢查其運行,下一個錯誤被拋出:如何堵塞txyam對龍捲風的頂部IOLoop

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() 

請幫我解決這個問題!

+0

爲什麼不爲memcached和龍捲風使用異步驅動程序?像這樣:https://github.com/dpnova/tornado-memcache – dpn

回答

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方法,當您實際允許使用YamClient實例時,該方法會返回一個Deferred。或者可能有另一個構造函數返回一個DeferredYamClient實例一起觸發,但只有在它可以使用後纔會觸發。

+0

謝謝。你絕對是寫作。問題在於buildProtocol方法。 connectTCP返回一個連接器實例,而buildProtocol異步執行。所以你可以給我一個例子,你如何解決這個問題。我試圖添加延遲,這將在客戶端和服務器之間的協議建立時執行。 –