[Twisted-Python] Question on deferreds

Frank Millman frank at chagford.com
Mon Dec 5 04:33:51 EST 2005


Ken wrote:
> 
> Hi Frank,
> 
> Best that I can tell, it's not your various classes that need 
> to be converted to deferreds, just your program that calls 
> class1, or even class1 itself.

Thanks for the reply.

> 
> The bottom line is that _something_ higher up the calling 
> stack needs to be aware that this is an asynchronous process 
> - and that a deferred gets generated along the way. I believe 
> it would be helpful if the originating function is coded for this.
> 

I understand what you are getting at, but I have spent a few hours trying to
squeeze this concept into my program, and I am not succeeding.

There are two main reasons that I can see.

Firstly, I oversimplified my pseudo code.

You suggested changing this

    class1():
        def check1(data):
            perform test
            if test failed:
                return False
            if not class2.check2(data):
                return False
            return True

to this

     class1():
         def check1(data):
             perform test
             if test failed:
                 return defer.succeed(False)
             return class2.check2(data)

I understand that, but I should have used this as an example -

    class1():
        def check1(data):
            perform test
            if test failed:
                return False
            if not class2.check2(data):
                return False
            do some extra stuff here  # this is what I left out
            return True

This 'extra stuff' can occur at any of the levels in the stack. I cannot see
where I could put this using your version.

Secondly, under some circumstances I loop over a number of objects and
validate them one after the other -

    class1():
        def check1(data):
            for obj in objList:
                if not check2(obj):
                    return False
            perform test
            if test failed:
                return False
            if not check2(data):
                return False
            do some extra stuff here
            return True

As you can see, it is getting even more complicated to do this using
deferreds.

The irony is that I actually want the client program to block until a
response is received, so maybe I should not be using Twisted on the client
at all. I could write a normal socket client, and use a ServerFactory on the
server.

I am very new to Twisted, so it is likely that my problem is that I have not
learned to think in a Twisted manner yet. Any advice to put me straight will
be much appreciated.

Thanks

Frank





More information about the Twisted-Python mailing list