start to learn python, can not get the return value from yield return. can anybody give me a favor.
I just want to pass a token from url to WSHandler, and get the uid from tonadoredis(github: https://github.com/leporo/tornado-redis) get method. and then blpop a list named "noticeT" + uid, when the "noticeTuid" has a new item, i want it return to the browser.
but my code can not work, how can I resove my problem?
class WSHandler(tornado.websocket.WebSocketHandler):
res = {'code':200, 'info': ''}
def initialize(self):
self.c = tornadoredis.Client(host=CONFIG['REDIS_HOST'], port=CONFIG['REDIS_PORT'], password=CONFIG['REDIS_AUTH'])
self.logintoken = self.get_argument('logintoken')
self.noticeModel = noticeModel(self.logintoken, self.c)
def open(self):
print 'new connection'
# uid = yield tornado.gen.Task(self.c.get, self.logintoken)
# print uid
self.res['info'] = self.noticeModel.getNotice()
if self.res['info']:
print self.res
self.write_message(json_encode(self.res))
else:
self.res['code'] = 500
self.res['info'] = 'error'
self.write_message(json_encode(self.res))
def on_message(self, message):
print 'message received %s' % message
def on_close(self):
print 'connection closed'
def check_origin(self, origin):
# print origin
# return True
parsed_origin = urlparse(origin)
# print parsed_origin
return parsed_origin.netloc.endswith("localhost")
and the noticeModel code is :
class noticeModel :
def __init__(self, logintoken, redisobj):
self.logintoken = logintoken
self.redisobj = redisobj
# @tornado.web.asynchronous
@tornado.gen.engine
def getNotice(self):
# uid = self.setLoginUid()
uid = yield tornado.gen.Task(self.redisobj.get, self.logintoken)
print "getNotice uid is %s"% uid
# if self.uid:
print "getNotice logintoken is %s"% self.logintoken
key = "noticeT" + uid
key = key.encode('utf-8')
print 'listening key:%s'% key
yield tornado.gen.Task(self.redisobj.blpop, key, 0)
@tornado.gen.engine
def setLoginUid(self):
yield tornado.gen.Task(self.redisobj.get, self.logintoken)