Python龙卷风redis - 如何做管道

时间:2012-05-14 12:44:02

标签: python redis tornado

龙卷风管道的正确方法是什么?文件不清楚。我正在使用tornadoredis图书馆。

    import tornadoredis

    @tornado.web.asynchronous
    @tornado.gen.engine
    def get(self):
        self.pipe_threads = []
        sw = stopwatch.Timer()
        if not self.get_cookie('rtbhui'):
            self.cookie_id=str(uuid4())
            self.set_cookie('test', self.cookie_id, domain='rtb.rtbhui.com',expires_days=365*2)
        else:
            self.cookie_id = self.get_cookie('test')

        t = yield tornado.gen.Task(c.pipeline)
        yield tornado.gen.Task(t.set,'key', 'value')
        yield tornado.gen.Task(t.execute)



starting
ERROR:root:Uncaught exception GET /apixel?bid=eydhdmlkJzogJ2luZm9AYWRtYWdpYy5jby5qcCcsICdjcmlkJzogJzIwNzY3MDczNTE1JywgJ21hYmlkJzogeydub2RlJzogTm9uZSwgJ2hvZCc6ICcxMycsICdjaWQnOiAnMzYnLCAnaW5kdXN0cnknOiBOb25lLCAnZXgnOiAnMScsICd2aWQxJzogJzI5JywgJ2Rvdyc6ICczJ30sICdwcmlkJzogeydob2QnOiAnMTMnLCAnd295JzogJzE4JywgJ2NpZCc6ICczNicsICdkb3cnOiAnMycsICdzc3AnOiAnYWR4JywgJ3N0JzogTm9uZSwgJ2J0JzogJ2ZpcmVmb3gnLCAnY3R5JzogJ3Rva3lvJywgJ2V4JzogJzEnLCAndmlkMic6ICcyMjInLCAnZHQnOiAnMScsICdvcyc6ICdtYWMnLCAndmlkMSc6ICcyOSd9LCAnYWdpZCc6ICc0NTQ3OTE3Nzk1JywgJ2Nvb2tpZWlkJzogJ3JldGFnZXRpbmc6Y29va2llJywgJ2RpZCc6ICd5YWhvby5jb20nLCAndmFsaWRhdGlvbic6IFRydWV9 (127.0.0.1)
HTTPRequest(protocol='http', host='127.0.0.1:8055', method='GET', uri='/apixel?bid=eydhdmlkJzogJ2luZm9AYWRtYWdpYy5jby5qcCcsICdjcmlkJzogJzIwNzY3MDczNTE1JywgJ21hYmlkJzogeydub2RlJzogTm9uZSwgJ2hvZCc6ICcxMycsICdjaWQnOiAnMzYnLCAnaW5kdXN0cnknOiBOb25lLCAnZXgnOiAnMScsICd2aWQxJzogJzI5JywgJ2Rvdyc6ICczJ30sICdwcmlkJzogeydob2QnOiAnMTMnLCAnd295JzogJzE4JywgJ2NpZCc6ICczNicsICdkb3cnOiAnMycsICdzc3AnOiAnYWR4JywgJ3N0JzogTm9uZSwgJ2J0JzogJ2ZpcmVmb3gnLCAnY3R5JzogJ3Rva3lvJywgJ2V4JzogJzEnLCAndmlkMic6ICcyMjInLCAnZHQnOiAnMScsICdvcyc6ICdtYWMnLCAndmlkMSc6ICcyOSd9LCAnYWdpZCc6ICc0NTQ3OTE3Nzk1JywgJ2Nvb2tpZWlkJzogJ3JldGFnZXRpbmc6Y29va2llJywgJ2RpZCc6ICd5YWhvby5jb20nLCAndmFsaWRhdGlvbic6IFRydWV9', 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:8055', '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'})
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/tornado-2.2.1-py2.7.egg/tornado/web.py", line 1074, in wrapper
    return method(self, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/tornado-2.2.1-py2.7.egg/tornado/gen.py", line 107, in wrapper
    runner.run()
  File "/usr/local/lib/python2.7/dist-packages/tornado-2.2.1-py2.7.egg/tornado/gen.py", line 319, in run
    yielded = self.gen.throw(*exc_info)
  File "/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbTornadoServer/tornadoServer.py", line 81, in get
    t = yield tornado.gen.Task(c.pipeline)
  File "/usr/local/lib/python2.7/dist-packages/tornado-2.2.1-py2.7.egg/tornado/gen.py", line 341, in run
    self.yield_point.start(self)
  File "/usr/local/lib/python2.7/dist-packages/tornado-2.2.1-py2.7.egg/tornado/gen.py", line 223, in start
    self.func(*self.args, **self.kwargs)
TypeError: pipeline() got an unexpected keyword argument 'callback'
ERROR:root:500 GET /apixel?bid=eydhdmlkJzogJ2luZm9AYWRtYWdpYy5jby5qcCcsICdjcmlkJzogJzIwNzY3MDczNTE1JywgJ21hYmlkJzogeydub2RlJzogTm9uZSwgJ2hvZCc6ICcxMycsICdjaWQnOiAnMzYnLCAnaW5kdXN0cnknOiBOb25lLCAnZXgnOiAnMScsICd2aWQxJzogJzI5JywgJ2Rvdyc6ICczJ30sICdwcmlkJzogeydob2QnOiAnMTMnLCAnd295JzogJzE4JywgJ2NpZCc6ICczNicsICdkb3cnOiAnMycsICdzc3AnOiAnYWR4JywgJ3N0JzogTm9uZSwgJ2J0JzogJ2ZpcmVmb3gnLCAnY3R5JzogJ3Rva3lvJywgJ2V4JzogJzEnLCAndmlkMic6ICcyMjInLCAnZHQnOiAnMScsICdvcyc6ICdtYWMnLCAndmlkMSc6ICcyOSd9LCAnYWdpZCc6ICc0NTQ3OTE3Nzk1JywgJ2Nvb2tpZWlkJzogJ3JldGFnZXRpbmc6Y29va2llJywgJ2RpZCc6ICd5YWhvby5jb20nLCAndmFsaWRhdGlvbic6IFRydWV9 (127.0.0.1) 6.69ms

2 个答案:

答案 0 :(得分:3)

Mybe有点太晚了,但仍有人可能偶然发现这个问题。

当您使用带有yieldtornado.gen.engine包装的tornado.gen.Task语句时,您会延迟执行一个低于屈服点的代码,直到操作(您已在{{ 1}}语句)完成。在大多数情况下,您将使用yield gen.Task(..)语句等待数据传输完成。

使用管道的要点是将多个redis命令打包到单个请求中,以减少对Redis服务器的大量请求。因此,当您向管道添加命令时,不会向服务器发送任何内容。此时无需使用yield语句。

调用yield方法时,之前打包到管道的所有命令都将发送到Redis服务器。即使您不想等待请求完成检查结果,最好使用execute包装execute方法调用。

使用yield gen.Task()方法的代码可能如下所示:

tornado-redis.Client.pipeline

或者您可以使用'with'语句:

client = Client()
pipe = client.pipeline()
pipe.hset('foo', 'bar', 1)
pipe.expire('foo', 60)

re1, res2 = yield gen.Task(pipe.execute)

管道功能使用Redis事务功能。您可以在本文中了解它们:http://redis.io/topics/transactions

答案 1 :(得分:1)

在您的情况下看起来像pipeline函数不能与gen.engine一起使用。

您是否尝试使用t = c.pipeline()代替t = yield tornado.gen.Task(c.pipeline)