[Twisted-Python] Re: Chaining deferreds with DeferredList

Tsai Li Ming mailinglist at ltsai.com
Sat Nov 13 08:38:36 MST 2004


I'm not too sure whether this works:

def cbTable(rows):
     dlist = []
     mt  = sqlreflector.SQLReflector(dbpool, [commentRow])
     if rows:
         for r in rows:
             d = mt.deleteRow(c)
             d.addCallback(cbDelete)
             d.addErrback(ebDelete)
             dlist.append(d)
     else:
         d = delete()
         dlist.append(d)

     dl = defer.DeferredList(dlist)
     return dl

Liming

Tsai Li Ming wrote:
> How do I chain a deferred object with multiple deferred objects within a 
> single function call?
> 
> I'm trying to wait for all the deferred to fire before triggering the 
> foo function. How can I chain up the multiple deferreds in the FOR loop?
> 
> for k in keys:
>     where = [("pk_col",reflector.EQUAL,k)]
>     d = mt.loadObjectsFrom("table",whereClause=where)
>     d.addCallback(cbTable)
>     d.addErrback(ebTable)
>     defer_list.append(d)
> 
> dl = defer.DeferredList(defer_list)
> dl.addCallback(foo)
> 
> def foo(result):
>     print result
>     reactor.stop()
> 
> def cbTable(rows):
> 
>     mt  = sqlreflector.SQLReflector(dbpool, [commentRow])
>     if rows:
>         for r in rows:
>             d = mt.deleteRow(c)
>             d.addCallback(cbDelete)
>             d.addErrback(ebDelete)
>     else:
>         d = delete()
> 
>     return d
> 
> 
> def cbDelete(result):
>     d = delete() # Returns a deferred
>     return d
> 
> 
> Thanks,
> Liming





More information about the Twisted-Python mailing list