[Twisted-Python] run queries in deffered, but not in transaction
Matt Perry
matt at unshift.net
Wed Sep 16 20:35:06 MDT 2009
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.
def logRequest(self, *arg, **kw):
obj = copy.deepcopy(kw['obj'])
d = self.db.runInteraction(obj.first)
d.addCallback(self.db.runInteraction, obj.second, param1, param2)
d.addErrback(log.err)
d.addCallback(self.db.runInteraction, obj.third)
d.addErrback(log.err)
Note that "d" is the same Deferred throughout. If you change it to:
@inlineCallbacks
def logRequest(self, *arg, **kw):
obj = copy.deepcopy(kw['obj'])
firstDeferred = self.db.runInteraction(obj.first)
yield firstDeferred
# code here won't get resumed until "firstDeferred" has a result
print firstDeferred.result
secondDeferred = self.db.runInteraction(obj.second)
yield secondDeferred
# same as above, code won't get executed until "secondDeferred" has a
result
then you'll get the behavior you're looking for.
See
http://twistedmatrix.com/documents/8.2.0/api/twisted.internet.defer.html#inlineCallbacksfor
some more information.
- Matt
On Wed, Sep 16, 2009 at 12:18 PM, Pet <petshmidt at googlemail.com> wrote:
> On Tue, Sep 15, 2009 at 6:21 PM, Pet <petshmidt at googlemail.com> wrote:
> > On Tue, Sep 15, 2009 at 5:19 PM, Mark Visser <markv at lumierevfx.com>
> wrote:
> >> exarkun at twistedmatrix.com wrote:
> >>> On 10:37 am, petshmidt at googlemail.com wrote:
> >>>>
> >>>> I'd like to run several queries in background, some of them may fail.
> >>>>
> >>>
> >>> If you have a function along the lines of this one:
> >>>
> >>> def someInteractions(db):
> >>> interactions = [
> >>> db.runInteraction(one),
> >>> db.runInteraction(two),
> >>> db.runInteraction(three)]
> >>>
> >>> Then a failure in one shouldn't affect two or three; likewise for any
> >>> other failure or combination of failures. They are naturally (ugh, not
> >>> a good word, but I can't think of a better one) independent. You have
> >>> to go out of your way to associate them somehow.
> >>>
> >> I think he might mean he wants them to run sequentially, even if one
> fails.
> >>
> >> You can do that explicitly via @inlineCallbacks like this:
> >>
> >> @inlineCallbacks
> >> def someInteractions(db):
> >> try:
> >> yield db.runInteraction(one)
> >> except:
> >> pass
> >>
> >> try:
> >> yield db.runInteraction(two)
> >> except:
> >> pass
> >>
> >> try:
> >> yield db.runInteraction(three)
> >> except:
> >> pass
> >>
> >> Or with callback/errbacks, like this:
> >>
> >> def someInteractions(db)
> >> d = db.runInteraction(one).addBoth(db.runInteraction,
> >> two).addBoth(db.runInteraction, three)
> >
>
> Hi,
>
>
> I've tried to do following:
>
> def logRequest(self, *arg, **kw):
> obj = copy.deepcopy(kw['obj'])
>
> d = self.db.runInteraction(obj.first)
>
> d.addCallback(self.db.runInteraction, obj.second, param1, param2)
> d.addErrback(log.err)
>
> d.addCallback(self.db.runInteraction, obj.third)
> d.addErrback(log.err)
>
>
> unfortunately it doesn't work in that way, because I suppose, obj is
> destroyed if second or third interaction starts.
> Is there a way to solve this?
>
> Thanks, Pet
>
> > Hi Mark!
> >
> > Yes, I'd like run them sequentially, it was not clear for me, how to
> > do it in one deferred.
> >
> > I will try your suggestions out.
> >
> > Thanks for help!
> >
> > Pet
> >
> >>
> >> addBoth is a convenience method that adds the same function as a
> >> callback and an errback:
> >>
> >>
> http://twistedmatrix.com/documents/8.2.0/api/twisted.internet.defer.Deferred.html#addBoth
> >>
> >> --
> >> Mark Visser, Software Director
> >> Lumière VFX
> >> Email: markv at lumierevfx.com
> >> Phone: +1-514-316-1080 x3030
> >>
> >>
> >> _______________________________________________
> >> Twisted-Python mailing list
> >> Twisted-Python at twistedmatrix.com
> >> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
> >>
> >
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20090916/8da2e45b/attachment.html>
More information about the Twisted-Python
mailing list