Make sure you're using the @inlineCallbacks decorator and the yield statement referenced previously. Without those you're just adding several callbacks to the same Deferred; with them, the function will wait until the Deferred fires before continuing.<br>
<br>def logRequest(self, *arg, **kw):<br>
obj = copy.deepcopy(kw['obj'])<br>
<br>
d = self.db.runInteraction(obj.first)<br><div id=":1s0" class="ii gt">
<br>
d.addCallback(self.db.runInteraction, obj.second, param1, param2)<br>
d.addErrback(log.err)<br>
<br>
d.addCallback(self.db.runInteraction, obj.third)<br>
d.addErrback(log.err)<br><br><br>Note that "d" is the same Deferred throughout. If you change it to:<br><br>@inlineCallbacks<br>def logRequest(self, *arg, **kw):<br>
obj = copy.deepcopy(kw['obj'])<br>
<br>
firstDeferred = self.db.runInteraction(obj.first)<br><div id=":1s0" class="ii gt">
yield firstDeferred<br><br> # code here won't get resumed until "firstDeferred" has a result<br> print firstDeferred.result<br> <br> secondDeferred = self.db.runInteraction(obj.second)<br> yield secondDeferred<br>
<br> # same as above, code won't get executed until "secondDeferred" has a result<br><br>then you'll get the behavior you're looking for.<br><br>See <a href="http://twistedmatrix.com/documents/8.2.0/api/twisted.internet.defer.html#inlineCallbacks">http://twistedmatrix.com/documents/8.2.0/api/twisted.internet.defer.html#inlineCallbacks</a> for some more information.<br>
<br> - Matt<br></div><br></div><br><br><div class="gmail_quote">On Wed, Sep 16, 2009 at 12:18 PM, Pet <span dir="ltr"><<a href="mailto:petshmidt@googlemail.com">petshmidt@googlemail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><div></div><div class="h5">On Tue, Sep 15, 2009 at 6:21 PM, Pet <<a href="mailto:petshmidt@googlemail.com">petshmidt@googlemail.com</a>> wrote:<br>
> On Tue, Sep 15, 2009 at 5:19 PM, Mark Visser <<a href="mailto:markv@lumierevfx.com">markv@lumierevfx.com</a>> wrote:<br>
>> <a href="mailto:exarkun@twistedmatrix.com">exarkun@twistedmatrix.com</a> wrote:<br>
>>> On 10:37 am, <a href="mailto:petshmidt@googlemail.com">petshmidt@googlemail.com</a> wrote:<br>
>>>><br>
>>>> I'd like to run several queries in background, some of them may fail.<br>
>>>><br>
>>><br>
>>> If you have a function along the lines of this one:<br>
>>><br>
>>> def someInteractions(db):<br>
>>> interactions = [<br>
>>> db.runInteraction(one),<br>
>>> db.runInteraction(two),<br>
>>> db.runInteraction(three)]<br>
>>><br>
>>> Then a failure in one shouldn't affect two or three; likewise for any<br>
>>> other failure or combination of failures. They are naturally (ugh, not<br>
>>> a good word, but I can't think of a better one) independent. You have<br>
>>> to go out of your way to associate them somehow.<br>
>>><br>
>> I think he might mean he wants them to run sequentially, even if one fails.<br>
>><br>
>> You can do that explicitly via @inlineCallbacks like this:<br>
>><br>
>> @inlineCallbacks<br>
>> def someInteractions(db):<br>
>> try:<br>
>> yield db.runInteraction(one)<br>
>> except:<br>
>> pass<br>
>><br>
>> try:<br>
>> yield db.runInteraction(two)<br>
>> except:<br>
>> pass<br>
>><br>
>> try:<br>
>> yield db.runInteraction(three)<br>
>> except:<br>
>> pass<br>
>><br>
>> Or with callback/errbacks, like this:<br>
>><br>
>> def someInteractions(db)<br>
>> d = db.runInteraction(one).addBoth(db.runInteraction,<br>
>> two).addBoth(db.runInteraction, three)<br>
><br>
<br>
</div></div>Hi,<br>
<br>
<br>
I've tried to do following:<br>
<br>
def logRequest(self, *arg, **kw):<br>
obj = copy.deepcopy(kw['obj'])<br>
<br>
d = self.db.runInteraction(obj.first)<br>
<br>
d.addCallback(self.db.runInteraction, obj.second, param1, param2)<br>
d.addErrback(log.err)<br>
<br>
d.addCallback(self.db.runInteraction, obj.third)<br>
d.addErrback(log.err)<br>
<br>
<br>
unfortunately it doesn't work in that way, because I suppose, obj is<br>
destroyed if second or third interaction starts.<br>
Is there a way to solve this?<br>
<br>
Thanks, Pet<br>
<div><div></div><div class="h5"><br>
> Hi Mark!<br>
><br>
> Yes, I'd like run them sequentially, it was not clear for me, how to<br>
> do it in one deferred.<br>
><br>
> I will try your suggestions out.<br>
><br>
> Thanks for help!<br>
><br>
> Pet<br>
><br>
>><br>
>> addBoth is a convenience method that adds the same function as a<br>
>> callback and an errback:<br>
>><br>
>> <a href="http://twistedmatrix.com/documents/8.2.0/api/twisted.internet.defer.Deferred.html#addBoth" target="_blank">http://twistedmatrix.com/documents/8.2.0/api/twisted.internet.defer.Deferred.html#addBoth</a><br>
>><br>
>> --<br>
>> Mark Visser, Software Director<br>
>> Lumière VFX<br>
>> Email: <a href="mailto:markv@lumierevfx.com">markv@lumierevfx.com</a><br>
>> Phone: +1-514-316-1080 x3030<br>
>><br>
>><br>
>> _______________________________________________<br>
>> Twisted-Python mailing list<br>
>> <a href="mailto:Twisted-Python@twistedmatrix.com">Twisted-Python@twistedmatrix.com</a><br>
>> <a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br>
>><br>
><br>
<br>
_______________________________________________<br>
Twisted-Python mailing list<br>
<a href="mailto:Twisted-Python@twistedmatrix.com">Twisted-Python@twistedmatrix.com</a><br>
<a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br>
</div></div></blockquote></div><br>