python - Return from @tornado.gen.engine function without yield? -
i'd execute or not execute async query based on value of parameter. if parameter true, query shouldn't executed.
i have method this:
@tornado.gen.engine def retrievesomedata(self, feelinglucky, callback): if feelinglucky: return # <-- doesn't work, function never returns! else: response = yield tornado.gen.task(queryfunction, param1....) callback(response)
how can make feelinglucky
branch work?
the thing can think of raising exception , catching in caller. that's ugly. or, if there such thing null task...
(python 2.7, tornado 3.2)
better use modern gen.coroutine
instead of obsolete gen.engine
. makes sort of conditional logic simple , natural:
@tornado.gen.coroutine def retrievesomedata(self, feelinglucky): if feelinglucky: return else: response = yield tornado.gen.task(queryfunction, param1....) raise gen.return(response)
if convert queryfunction coroutine style too, get:
@tornado.gen.coroutine def retrievesomedata(self, feelinglucky): if feelinglucky: return else: response = yield queryfunction(param1....) raise gen.return(response)
Comments
Post a Comment