When I use get_current_user() I need to check few things in Redis (use tornado-redis) asynchronously.
I am doing the following:
def authenticated_async(method):
@gen.coroutine
def wrapper(self, *args, **kwargs):
self._auto_finish = False
self.current_user = yield gen.Task(self.get_current_user_async)
if not self.current_user:
self.redirect(self.reverse_url('login'))
else:
result = method(self, *args, **kwargs) # updates
if result is not None:
yield result
return wrapper
class BaseClass():
@gen.coroutine
def get_current_user_async(self,):
auth_cookie = self.get_secure_cookie('user') # cfae7a25-2b8b-46a6-b5c4-0083a114c40e
user_id = yield gen.Task(c.hget, 'auths', auth_cookie) # 16
print(123, user_id)
return auth_cookie if auth_cookie else None
For example, I want to use authenticated_async decorator:
class IndexPageHandler(BaseClass, RequestHandler):
@authenticated_async
def get(self):
self.render("index.html")
But I have in console only 123.
Whats wrong? How to fix that?
Thanks!
UPDATE
I have updated the code with yield result
.
In auth_cookie I have cfae7a25-2b8b-46a6-b5c4-0083a114c40e
.
Then I go to terminal:
127.0.0.1:6379> hget auths cfae7a25-2b8b-46a6-b5c4-0083a114c40e
"16"
So,
user_id = yield gen.Task(c.hget, 'auths', auth_cookie)
print(123, user_id)
Must return
123 16
But it returns one 123
UPDATE 1
With
class IndexPageHandler(BaseClass, RequestHandler):
@gen.coroutine
def get(self):
print('cookie', self.get_secure_cookie('user'))
user_async = yield self.get_current_user_async()
print('user_async', user_async)
print('current user', self.current_user)
self.render("index.html",)
In console I have:
cookie b'cfae7a25-2b8b-46a6-b5c4-0083a114c40e'
123
user_async b'cfae7a25-2b8b-46a6-b5c4-0083a114c40e'
current user None
authenticated_async
's wrapper (result = method(*args, **kwargs); if result is not None: yield result
). That doesn't apply to thisIndexPageHandler
, though. If you're getting to the print statement it looks like all the async stuff is working and you're just getting an empty result fromc.hget
(whatever that is). If that's not it can you be more clear about what exactly you're expecting and what you're seeing instead?get_secure_cookie
andc.hget
(orget_current_user_async
) inhandler.get
directly?@gen.coroutine
and useyield
when callingget_current_user_async()
.