[Twisted-Python] Deferred problem

Andrew Bennetts andrew-twisted at puzzling.org
Thu Nov 6 19:49:19 EST 2003


On Thu, Nov 06, 2003 at 09:26:00AM -0500, vicky lupien wrote:

[reordered slightly]
> 
>        def initPermissions(self, request):       #create and return the results of a sql query
>            group = request.getComponent(myguard.Authenticated).group
>            listGroup = group.split(',')
>            sql = "select permission from permissions where 1"
>            for item in listGroup:
>                sql += " or permissions.group='%s'" % item.lstrip()
>            theDeferred = self.dbpool.runQueryDict(sql)
>            return theDeferred.addCallback(self.makeDict)        # suppose to call makeDict when the results has arrived
> 
> 
>        def makeDict(self, results):
>                # when I print what is in results, I can see all the data from the sql query
>            listPermissions = []
>            for item in results:
>                listPermissions.append(item.get('permission'))
>            return listPermissions                   # return the new list but the function setUp receive a deferred
> 
>        def setUp(self, request, *args):
>            var = self.initPermissions(request)      #var is suppose to get the new list
>            print var

setUp is receiving a Deferred from self.initPermissions, but then doesn't
wait for its result to arrive.  You should instead do something more like:

        def setUp(self, request, *args):
            d = self.initPermissions(request)
            def printVar(var):
                print var
            d.addCallback(printVar)

Although ideally you should add an errback too, even if it is just
twisted.python.log.err.

(I don't know how this will interact with the rest of Woven, but hopefully
I've clarified how you should be using Deferreds a little)

-Andrew.





More information about the Twisted-Python mailing list