1

I have 2 python classes in two files.

  • file: LdapConnection, class: LdapClass(), method : getMachines(self)

    @defer.inlineCallbacks
    def getMachines(self):
        c = ldapconnector.LDAPClientCreator(reactor, ldapclient.LDAPClient)
        overrides = {self.basedn: (self.serverip, 389)}
        client = yield c.connect(self.basedn, overrides=overrides)
        yield client.bind(self.binddn, self.bindpw)
        o = ldapsyntax.LDAPEntry(client, self.basedn)
        results = yield o.search(filterText=self.query)
        for entry in results:
            for i in entry.get('name'):
                self.machineList.append(i)
    
        yield self.machineList
        print self.machineList
        return
    

above print statements print all entires in the machineList

  • file:Twisted, class:Caching, method:loadSettings(self)

    @defer.inlineCallbacks 
    def loadSettings(self):
    
        returned =  yield LdapClass().getMachines()  
        print returned
    

in the above class my print prints None. What Im doing wrong here?

Sraw
  • 18,892
  • 11
  • 54
  • 87
Ratha
  • 9,434
  • 17
  • 85
  • 163
  • I have tried to format your code but it is really broken before. So I am not sure am I right. Please check. – Sraw Oct 18 '17 at 03:52
  • Possible duplicate of [What is the result of a yield expression in Python?](https://stackoverflow.com/questions/10695456/what-is-the-result-of-a-yield-expression-in-python) – Aaron Oct 18 '17 at 03:55
  • Can you reduce your example to a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve)? It's difficult to get the exact output you see. Your use of yield is curious indeed. The combination of `yield` and `return` looks suspect, as does returning yielded values from functions and yielding lists rather than individual items. I'd expect looped yields or `yield from` although I realise you are using py2 which doesn't have `yield from`. – Paul Rooney Oct 18 '17 at 04:32
  • The answer to this question is included in on the other similar question you asked about this code. https://stackoverflow.com/questions/46801724/how-to-assign-a-returned-value-from-the-defer-method-in-python-twisted – Jean-Paul Calderone Oct 18 '17 at 11:01

2 Answers2

2

In getMachines(), don't yield self.machineList.

In a method decorated by defer.inlineCallbacks, what yield does is yield execution until the argument (a defer.Deferred object) calls back with a value (if it's not a defer.Deferred, it'll simply continue). (It's similar to the new await keyword in Python 3.) You are using this correctly in loadSettings().

In getMachines(), you don't have a defer.Deferred; you have a list, so it continues. To return that value to the caller, call returnValue(self.machineList) at the end of the method.

1

Looks like your loadSettings() routine is yielding the getMachines() generator, not the results from the generator. Perhaps the former should be doing “yield from”?

Lawrence D'Oliveiro
  • 2,768
  • 1
  • 15
  • 13