Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twisted inlineCallbacks and remote generators

I have used defer.inlineCallbacks in my code as I find it much easier to read and debug than using addCallbacks.

I am using PB and I have hit a problem when returning data to the client. The data is about 18Mb in size and I get a failed BananaError because of the length of the string being returned.

What I want to do is to write a generator so I can just keep calling the function and return some of the data each time the function is called.

How would I write that with inlineCallbacks already being used? Is it actually possible, If i return a value instead. Would something like the following work?

@defer.inlineCallbacks
def getLatestVersions(self):
    returnlist = []
    try:
        latest_versions = yield self.cur.runQuery("""SELECT id, filename,path,attributes ,MAX(version) ,deleted ,snapshot , modified, size, hash, 
                           chunk_table, added, isDir, isSymlink, enchash from files group by filename, path""")
    except:
        logger.exception("problem querying latest versions")

    for result in latest_versions:
        returnlist.append(result)
        if len(return_list) >= 10:
            yield return_list
            returnlist = [] 
    yield returnlist
like image 453
Deano123 Avatar asked Jan 01 '14 14:01

Deano123


1 Answers

A generator function decorated with inlineCallbacks returns a Deferred - not a generator. This is always the case. You can never return a generator from a function decorated with inlineCallbacks.

See the pager classes in twisted.spread.util for ideas about another approach you can take.

like image 138
Jean-Paul Calderone Avatar answered Sep 18 '22 23:09

Jean-Paul Calderone