I have a simple handler like so :
@asynchronous
@gen.engine
def post(self):
result = functionOne()
foo = MyObject()
bar = AnotherObject()
But I’d like for result to come back, then execute foo, then bar in a sequence. I’ve tried using nested callbacks as per http://www.tornadoweb.org/documentation/gen.html but without much luck.
thx!
You are using the gen.engine, but you need to make your method calls into
yieldwith agen.Taskcalls.Example from a project of mine:
The above will first do the
chargepart then return the result and then call thescheduleand return thesubscriptionobject from that call.The methods that you call with the
gen.Taskneed to look like the following:Where
callbackwill be the function to call to actually return the value from thegen.Task.I have it setup to work with or without the callback so I can more easily test my methods. Which is just a simple test if
callbackisNoneor not and if it isn’tNonethen it gets called else it returns normally.gen.Engine Docs
EDIT:
Ok, to clarify the above it a complete function that works with
gen.Taskslooks like the following.The above when used in a
gen.Taskwill look like the following.This means to call the
delete_customerwith the parametercustomer_idand then I want the result of this function, the return, to be given toresult. So, when thedelete_customerfunction completes and sends theresultof theself.customerService.deleteto thecallbackthegen.Taskwill then return the value and store it inresult.Now, you can use the
resulthowever you wish, in my example it is returned.For you example it looks like you want to do the following.
This will first call the
functionOnethen go to the nextgen.Taskand execute theMyObjectand so forth. But, you must follow the pattern where you have acallbackparameter in your methods in order to usegen.Taskcorrectly otherwise you cannot get the result back.This is where my second, confusing example, comes into play.
In order to get around this requirement and to allow my code to work with
gen.Taskand without I do the following.This allows me to use the code in a lot more situations without breakage.