python - Why are these decorators called from outermost to innermost, not the other way round? -


i have following method inside tornado.websocket.websockethandler subclass:

@authenticate_user_async @gen.coroutine def on_message(self, data):     """     callback when new message received via socket.     """     # stuff... 

the @authenticate_user_async decorator below:

def authenticate_user_async(f):     """     authentication decorator based on http://tornadogists.org/5251927/      authenticates user's credentials , sets self.current_user if valid     """     @functools.wraps(f)     @gen.coroutine     def wrapper(self, *args, **kwargs):         # stuff...          f(self, *args, **kwargs)      return wrapper 

i thought innermost decorator called first, have expected need them given:

@gen.coroutine @authenticate_user_async def on_message(self, data):    ... 

but first version works , 1 above never called if yield keyword inside on_message() method.

why necessary use first version , not second?

(i think gave bug in answer previous question, sorry.)

the line in authenticate_user_async calls f needs changed. change this:

f(self, *args, **kwargs) 

to this:

yield f(self, *args, **kwargs) 

now swap order of decoration on_message:

@authenticate_user_async @gen.coroutine def on_message(self, data): 

in authenticate_user_async, "f" on_message. these 2 fixes ensure "f" coroutine , gets run completion.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -