gpt4 book ai didi

python - Tornado -redis : LPOP works but BLPOP doesn't?

转载 作者:可可西里 更新时间:2023-11-01 11:38:06 24 4
gpt4 key购买 nike

Tornado 和 Redis 的新手我发现这里有人有同样的问题,tornado-redis: RPOP works but BRPOP doesn't?但我仍然不明白为什么,以及如何解决我的问题

代码吹得很好

#coding:utf-8
import random
import time
import tornado.web
import tornado.httpserver
import tornado.ioloop
import tornado.options
from uuid import uuid4
# import redis
from tornado.escape import json_encode
import tornado.gen
import tornadoredis

class noticePush(tornado.web.RequestHandler):

def initialize(self):
print 'initialize'

@tornado.web.asynchronous
@tornado.gen.engine
def get(self):
print 'go here'
try:

**uid = self.get_argument('uid')
# key = u'test_comet%s'%uid
key = 'test_comet1'
c = tornadoredis.Client(host='127.0.0.1', port=6379,password='psw')
print key
res = yield tornado.gen.Task(c.blpop, key, 0)**
print res
if res :
self.finish(json_encode(res))
else :
self.finish('None')

except Exception, e :
print e
pass


class Application(tornado.web.Application):
def __init__(self):

handlers = [
(r'/', noticePush)
]

settings = {
'template_path': 'templates',
'static_path': 'static',
'debug': True
}

tornado.web.Application.__init__(self, handlers, **settings)

if __name__ == '__main__':
tornado.options.parse_command_line()

app = Application()
server = tornado.httpserver.HTTPServer(app)
server.listen(8000)
tornado.ioloop.IOLoop.instance().start()

但是,我尝试使用 get_argument 作为 key ,blpop 永远不会返回任何数据

**uid = self.get_argument('uid')
key = 'test_comet' + uid
c = tornadoredis.Client(host='127.0.0.1', port=6379, password='psw')
print key
res = yield tornado.gen.Task(c.blpop, key, 0)**
print res
if res :
self.finish(json_encode(res))
else :
self.finish('None')

最佳答案

我尝试阅读 tornadoredis 代码,找到 blpop def 并找到原因

def blpop(self, keys, timeout=0, callback=None):
tokens = to_list(keys)
tokens.append(timeout)
self.execute_command('BLPOP', *tokens, callback=callback)

def to_list(source):
if isinstance(source, str):
return [source]
else:
return list(source)

重要的是str_key = 'test_comet', 输入 (key) -> strunicode_key = 'test_comet' + uid , type (key) -> unicode

当我对 unicode_key.encode('utf-8') 进行编码时,代码有效!

关于python - Tornado -redis : LPOP works but BLPOP doesn't?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28968687/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com