开发者

Twisted, Deferred Callbacks chain

After reading the documentation and a few use cases. The question arose how to use a chain of Callbacks. More precisely how to transfer data between Callbacks

Here is the code:

d = defer.Deferred()
d.addCallback(self.min_active_stack)
d.addCallback(self.min_normalization)
d.addCallback(self.insert_db)
d.callback(0)

def min_active_stack(self,d):
    ...
    return self.dbconn.runQuery(sql_query)

def min_normalization(self,min):
    ...
    return min[0][0]+self.x

def insert_db(s开发者_高级运维elf,min_norm):
    ...
    return self.dbconn.runQuery(sql_query)

First, in min_active_stack I do request to db. In min_normalization I process the data. And insert_db on the processed data I do have one request to the db.

In this case, data is transmitted through the chain and it's simple. But what if in the middle of the chain need to run more then a Callback.

d = defer.Deferred()
d.addCallback(self.min_active_stack)
d.addCallback(self.stack_shift)
d.addCallback(self.min_normalization)
d.addCallback(self.insert_db)
d.callback(0)

def stack_shift(self, d):
    return self.dbconn.runQuery(query)

In stack_shift are not used any external data, but it should be run after min_active_stack and before insert_db. It turns out that in min_normalization comes from deferred stack_shift instead of min_active_stack.

For myself, I solved this problem by adding a line in stack_shiftt:

self.temp=d   

And use self.temp in min_normalization.

But as far as the right decision?


I actually don't understand the problem. Why you can't just pass result from min_active_stack() through stack_shift() into min_normalization()?

def stack_shift(res):
    # do something usefull
    return res

Or since it looks like you want to do something asynchronous in stack_shift, the Deferred equivalent:

def stack_shift(res):
    d = runQuery(query)
    d.addCallback(lambda ignored: res)
    return d
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜