[Twisted-Python] Question about deferreds

Doug Farrell dfarrell at mypublisher.com
Tue May 5 07:48:25 MDT 2009


Hi all,

 

I consider myself a Twisted "newbie", so this might seem like a simple
couple of questions, hope you'll indulge me.

 

When I first began experimenting with deferreds I was breaking up tasks
into sequences of steps something like this:

 

d = Deferred()

d.succeed(True)

d.addCallback(step1)

d.addCallback(step2)

d.addCallback(step3)

etc.

 

When this runs I was under the mistaken impression that at the end of
each callback (step1, step2, step3, etc) the deferred would yield back
to the main loop (reactor) and allow other deferreds to run. Now I know
that the callback functions run sequentially one after another with no
break. So my first question is this: what is the advantage of this over
just combining the callback functions into one big function?, something
like this:

 

d = Deferred()

d.succeed(True)

d.addCallback(OneBigStep)

 

If step1(), step2() and step3() take a fair bit of time to complete,
doesn't this effectively block the reactor loop for the entire exection
of all the steps?

 

My second question is related to this; what is a recommended way to
create the cooperative state machine I'm thinking of using deferreds?
For example if I create something like this:

 

d1 = Deferred()

d1.succeed(True)

d1.addCallback(d1_step1)

d1.addCallback(d1_step2)

d1.addCallback(d1_step3)

 

d2 = Deferred()

d2.succeed(True)

d2.addCallback(d2_step1)

d2.addCallback(d2_step2)

d2.addCallback(d2_step3)

 

How do I get the execution of this to be this:

 

1)      d1_step1

2)      d2_step1

3)      d1_step2

4)      d2_step2

5)      d1_step3

6)      d2_step3

 

I want to have the state machines I create cooperatively 'multi-task',
yielding control back to the main loop at each state transition.

 

Thanks in advance for any help, guidance or references you can provide.
I really enjoy Twisted, I'm still working on getting the 'Zen' of it.

 

Doug

-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20090505/f4da5d28/attachment.html>


More information about the Twisted-Python mailing list