Count of deleted entities on app engine using db.delete(iterator_of_keys)

时间:2015-06-25 18:23:09

标签: python google-app-engine bigtable

I find the entities I want to delete:

to_delete_iterator = models.Foo.all().filter('bar =', baz).run(keys_only = True, batch_size = 1000)

Then I delete them

db.delete(to_delete_iterator)

Which seems to work fine. But I'd like to log the number of entities that I deleted. Is there a way to get that result from db? If not, I think my only option is to loop over to_delete_iterator, build a list, takes it's length, and then pass that to db.delete. Any better options that this?

to_delete_keys = []
for key in to_delete_iterator:
  to_delete_keys.append(key)

logging.debug('>> will delete ' + str(len(to_delete_keys)) + ' old entities')   
db.delete(to_delete_keys)  

Presumably it uses a little more memory than passing the iterator to db.delete, but for keys_only that may not be an issue

1 个答案:

答案 0 :(得分:0)

不确定这会有多好,但你可以考虑使用tee。

from itertools import tee
k1, k2 = tee(models.Foo.all().filter('bar =', baz).run(keys_only = True, batch_size = 1000)) 

delete_future = db.delete_async(k2)
count = sum(k1)
delete_future.get_result()