-1

Since I am using NDB for my projects, on Python 2.7 SDK, I cannot use Query().iter() normally with Jinja2 templates.

My usual code follows this pattern:

mc_key = ...
instances = memcache.get('instances-%s' % mc_key)
if not instances:
    q_i = MyModel.query(...)
    instances = q_i.iter()
    if instances:
        memcache.set('instances-%s' % mc_key, instances)

And then in Jinja2:

{% for i in instances %}
    {{i.property1}}
{% endfor %}

This also happens when I call the QueryIterator object in the Python code. Adding to the previous Python code:

for i in instances:
    # Do something with i

I always get a "deadlock waiting for <future created by XXX, pending>" when I loop the iterator.

A working walkaround is:

q_i = MyModel.query()
instances = q_i.iter()
if instances:
    instances = list(instances)

Someone knows why that next() does not work as I expected? Some more elegant and/or efficient solution? Thank you in advance.

1 Answers1

0

As you can read in the docs: NDB uses Memcache as a cache service for "hot spots" in the data. If the application reads some entities often, NDB can read them quickly from cache. In other words, the caching is automatic.

Also, the elegant way to get a list of entities is provided by the method fetch()

instances = MyModel.query().fetch(20)

For example, instances in this line is a list of 20 entities, and this list has already been written into the cache.

Gianni Di Noia
  • 1,577
  • 10
  • 25
  • Ok, will use automatic MC from now on. For the iter() vs fetch() issue, AFAIK DB run() and NDB iter() are the standard and recommended methods, and fetch() is only a wrap around run() and iter(). Is there a way to use iter() IteratorObject in a loop, whithout before making it a list or other iterator? – Marcos Manuel Ortega Jul 03 '14 at 15:13