moby moby - 9 months ago 47
Python Question

ndb.get_multi() returning null entities

I want to use

to avoid doing queries which are more expensive. If my understanding is correct,
only charges you 1 read per entity returned, so if you have a list of 50 keys, and only 20 are found, you should only get charged for 20 reads.

keys = list()

keys.append(Key('User', 2)) // this user exists
keys.append(Key('User', 12)) // this user DOES NOT exist
keys.append(Key('User', 15)) // this user DOES NOT exist

users = ndb.get_multi(keys)

What I get back is 3 items, 1 with a user, and 2 with just null values. When I check AppStats, I see that it does indeed charge me for all the keys I put in there, even though 2 did not exist.

Why am I getting null entities back? Shouldn't items not found just be skipped?

Answer Source

You want to be able to do

obj1, obj2, obj3 = ndb.get_multi([key1, key2, key3])

If the number of results returned is of arbitrary length, the tuple unpacking breaks and you have to get a list and inspect and compare keys. It's also simple to remove None should you need it using filter:

filter(None, list)

The cost is per get/read (operation), which is not the same as per returned result (data). The cost of internal services and communications in GAE are roughly the same regardless of whether it exists, but you don't have to pay for cpu for deserialization for items that doesn't exist.