python - Twisted, Deferred Callbacks chain -


After reading the documents and some use cases, the question arises how to use a series of callbacks. More accuracy to transfer data between callback

Here is the code:

  d = defer.Deferred () d.addClallback (self.min_active_stack) d.addClallback (Self) .min_normalization) d.addClallback (self.insert_db) d.callback (0) def min_active_stack (auto, D): ... self-self.dbconn.runQuery (sql_query) def min_normalization (auto, min): .. Return [0] [0] + self.x def insert_db (auto, min_norm): ... back self.dbconn.runQuery (sql_query)   

First of all, in min_active_stack I I request for DB. In Min_normalization I process the data and insert_db on processed data is a request for DB.

In this case, the data is transmitted through the series and it is easy. But what if the callback will have to run more in the middle of the chain?

  d = defer.Deferred () d.addClallback (self.min_active_stack) d.addClallback (self.stack_shift) D.addCallback (self.min_normalization) d.addClallback (self.insert_db) d .callback (0) def stack_shift (self, d): returns self.dbconn.runQuery (query)   

No external data is used in stack_shift, but it is min_active_stack and insert_db Must move before. It has come to know that min_normalization comes from deferred stack_shift instead of min_active_stack.

For me, I resolved this problem by adding a line in stack_shiftt:

  self.temp = d   

and min_normalization Use self.temp in

But as far as the right decision is?

I really can not understand the problem. Why can not you just remove the result from min_active_stack () from min_normalization () via stacc_shift ()?

  def stack_shift (res): # Receive some useful returns   

Or do you think you want to do asynchronous work in stack_shift, deferred equivalent:

  def stack_shift (res): d = runQuery (query) d.addClallback (Lambda is ignored: Res) Return d    

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -