新手要扭曲和旋风 - 如何让redis回调工作的一个简单的获取请求

问题描述:

下面是我的代码尝试从获取请求写入到redis的非阻塞方法redis。下面还有我得到的错误。它是一个500错误。我只是不明白txredisapi随飓风一起发货的文档。它没有写入Redis的,但应用程序新手要扭曲和旋风 - 如何让redis回调工作的一个简单的获取请求

import cyclone.web 
import sys 
from twisted.internet import reactor 
from twisted.python import log 
import cyclone.redis as redis 
from twisted.internet import defer 
from twisted.internet import reactor 

@defer.inlineCallbacks 
def redisPixelWrite(remote_ip): 
    rc = yield redis.Connection() 
    yield rc.set("foo", 'itwodasdfred') 
    v = yield rc.get("foo") 
    print "foo:", v 
    yield rc.disconnect() 

class Application(cyclone.web.Application): 
    def __init__(self): 
     handlers = [ 
      (r"/", MainHandler), 
     ] 
     settings = {"xheaders": True,"static_path": "./static"} 
     cyclone.web.Application.__init__(self, handlers, **settings)  

class MainHandler(cyclone.web.RequestHandler): 
    def get(self): 
     remote_ip = self.request.remote_ip 
     redisPixelWrite(remote_ip).addcallback() 
     self.set_header('Content-Type', 'text/html') 
     self.write(remote_ip) 

def main(): 
    log.startLogging(sys.stdout) 
    reactor.listenTCP(8051, Application(), interface="127.0.0.1") 
    reactor.run() 

if __name__ == "__main__": 
    main() 


2012-05-07 21:12:10+0800 [-] Log opened. 
2012-05-07 21:12:10+0800 [-] Application starting on 8051 
2012-05-07 21:12:10+0800 [-] Starting factory <__main__.Application instance at 0x228af38> 
2012-05-07 21:12:14+0800 [HTTPConnection,0,127.0.0.1] Starting factory <cyclone.redis.RedisFactory instance at 0x261a680> 
2012-05-07 21:12:14+0800 [HTTPConnection,0,127.0.0.1] Uncaught exception GET/(127.0.0.1) :: HTTPRequest(protocol='http', host='127.0.0.1:8051', method='GET', uri='/', version='HTTP/1.1', remote_ip='127.0.0.1', body='', headers={'Connection': 'keep-alive', 'Accept-Language': 'en-us,en;q=0.5', 'Accept-Encoding': 'gzip, deflate', 'Cache-Control': 'max-age=0', 'Host': '127.0.0.1:8051', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:11.0) Gecko/20100101 Firefox/11.0'}) 
2012-05-07 21:12:14+0800 [HTTPConnection,0,127.0.0.1] 500 GET/(127.0.0.1) 2.07ms 
2012-05-07 21:12:14+0800 [RedisProtocol,client] foo: itwodasdfred 
2012-05-07 21:12:14+0800 [RedisProtocol,client] Stopping factory <cyclone.redis.RedisFactory instance at 0x261a680> 
+0

“但应用程序”是什么? –

这条线:

redisPixelWrite(remote_ip).addcallback() 

提高AttributeError。您应该提交一份针对Cyclone的错误报告,因为它应该在某处报告此错误(或者,确保您确实在查看将报告例外情况的日志)。

redisPixelWrite返回Deferred,因为它用inlineCallbacks装饰。 Deferred上没有addcallback方法。有一个addCallback方法,我想这可能是你想要使用的。它至少需要一个参数,但不是零,所以你需要修复它。

你错过了代码中的一些东西。

  1. get功能必须与inlineCallbacks装饰,它应该yield redisPixelWrite(remote_ip)
  2. 你可能不希望做一个新的连接到Redis的每个请求。设置持久连接池更有效,并在新请求中重新使用它。

这可能是你在找什么:

import cyclone.redis 
import cyclone.web 
import sys 

from twisted.python import log 
from twisted.internet import defer 
from twisted.internet import reactor 


class RedisMixin(object): 
    redis = None 

    @classmethod 
    def setup(self): 
     RedisMixin.redis = cyclone.redis.lazyConnectionPool() 


class Application(cyclone.web.Application): 
    def __init__(self): 
     handlers = [(r"/", MainHandler)] 
     RedisMixin.setup() 
     settings = {"debug": True, "xheaders": True, "static_path": "./static"} 
     cyclone.web.Application.__init__(self, handlers, **settings) 


class MainHandler(cyclone.web.RequestHandler, RedisMixin): 
    @defer.inlineCallbacks 
    def get(self): 
     try: 
      yield self.redis.set("foo", self.request.remote_ip) 
      ip = yield self.redis.get("foo") 
     except: 
      raise cyclone.web.HTTPError(503) # Service Unavailable 
     else: 
      self.set_header('Content-Type', 'text/html') 
      self.write(ip) 


def main(): 
    log.startLogging(sys.stdout) 
    reactor.listenTCP(8051, Application(), interface="127.0.0.1") 
    reactor.run() 

if __name__ == "__main__": 
    main() 

注意,所有Connection方法的默认行为是自动重拨应该Redis的停止,或重新启动。它将使用HTTP 503(服务不可用)进行响应,直到连接重新建立并且恢复与redis的通信。