waigani
waigani

Reputation: 3580

GAE ndb future.done() never returns true

The following recursive function loops infinitely because f.done() never returns true. Why does .done() never return true?

        def check(f):
            if f.done():
                logging.info(f.get_result())
            else:
                check(f)

        f = ndb.Key(Entity, 'id').get_async()
        #loops forever
        check(f)

Upvotes: 2

Views: 173

Answers (2)

Guido van Rossum
Guido van Rossum

Reputation: 16890

The code that is to make the future "done" will never get a chance to run because you never yield from the task or call get_result() or wait() on anything.

NDB's async processing doesn't use threads -- it interleaves code in a single thread and only switches between tasks when certain calls are made, such as wait(), get_result(), or yield .

Upvotes: 3

Aesthete
Aesthete

Reputation: 18848

It doesn't seem possible that this will loop forever. At most, it will loop for 60 seconds, which is the default wait time argument for the get_async method.

Also, unless this is a fairly simple example to illustrate a point, your recursion is not necessary. The get_result method will do all that for you, wrap it in a try - except and catch the exception if the query didn't succeed.

get_result() - Waits if necessary; then returns the result or raises the exception.

Upvotes: 1

Related Questions