我创建了一个页面来删除数据存储区中的所有条目。我正在使用self.key.delete()
,但它已经停止工作(它工作了一次,但它不再起作用了。)
我的python
代码删除条目:
class DeletePage(Handler):
def get(self):
self.key.delete()
self.render('deletepage.html')
答案 0 :(得分:2)
假设:
ndb
库models.py
文件然后你可以尝试这样的东西,连接到app的一个处理程序:
from google.appengine.ext import ndb
import inspect
import models
for kind, model in inspect.getmembers(models):
if not isinstance(model, ndb.model.MetaModel):
continue
cursor = None
while True:
keys, next_cursor, more = \
model.query().fetch_page(500, keys_only=True, start_cursor=cursor)
if keys:
ndb.delete_multi_async(keys)
if more and next_cursor:
cursor = next_cursor
else:
break
如果你有很多实体,那么上面可能会在DeadlineExceededError
之后被杀死(之后它应该删除了一堆实体)。要么重复请求,直到它们全部消失。
或者甚至可能尝试将工作拆分为延迟任务,在时间上错开,以便没有太多可能导致实例爆炸的同时请求。像这样:
from google.appengine.ext import deferred
from google.appengine.ext import ndb
import inspect
import models
def delete_keys(keys):
ndb.delete_multi(keys)
delay = 0
for kind, model in inspect.getmembers(models):
if not isinstance(model, ndb.model.MetaModel):
continue
cursor = None
while True:
keys, next_cursor, more = \
model.query().fetch_page(500, keys_only=True, start_cursor=cursor)
if keys:
deferred.defer(delete_keys, keys, _countdown=delay)
delay += 5
if more and next_cursor:
cursor = next_cursor
else:
break