python - How to move db request (which uses yield) in an other function? -


i'm playing tornado , mongodb, asynchronous driver motor. when working callbacks everything's fine. discovered possibility use motor.op or tornado.gen.task perform request in 1 function only:

so working:

class contact_handler(main_handler):      @web.asynchronous     @gen.coroutine     def get(self, other_id):          event = events.event_send_contact_request(self.user_id)         result = yield motor.op(db.users.update,                      {'_id': objectid(other_id)},                      {'$push': {'evts': event.data}}                     )          self.finish("ok") 

but i'd move database request in own function in module. problem don't understand how yield working here (despite read lot of questions yield). tried, it's not working:

#------ file views.py -------------  class contact_handler(main_handler):      def get(self, other_id):          event = events.event_send_contact_request(self.user_id)         result = model.push_event_to_user(other_id, event)          self.finish("ok") 

and call in function:

#------ file model.py -------------  @gen.coroutine def push_event_to_user(user_id, event):      ## ajout de la demande dans les events du demandé:     yield motor.op(db.users.update,                      {'_id': objectid(user_id)},                      {'$push': {'evts': event}}                     ) 

if investigate pdb:

(pdb) l 157             event = events.event_send_contact_request(self.user_id) 158             result = model.push_event_to_user(other_id, event) 159              160             import pdb; pdb.set_trace() 161              162  ->         self.finish("ok") 163           (pdb) result <tornado.concurrent.tracebackfuture object @ 0xa334b8c> (pdb) result.result() *** exception: dummyfuture not support blocking results 

any appreciated, thanks.

i found way of doing this, this post uses of tornado.gen.return. still need yield in main function, coroutine simple.

here code now:

#------ file views.py -------------  @web.asynchronous @gen.coroutine class contact_handler(main_handler):      def get(self, other_id):          event = events.event_send_contact_request(self.user_id)         result = yield model.push_event_to_user(other_id, event)          self.finish("ok") 

and call in function:

#------ file model.py -------------  @gen.coroutine def push_event_to_user(user_id, event):      ## ajout de la demande dans les events du demandé:     result = yield motor.op(db.users.update,                             {'_id': objectid(user_id)},                             {'$push': {'evts': event}}                            )     raise gen.return(result) 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -