Python 2.7 ndb put()工作,但在15-30秒后不会同时更新结果
我有一个非常有趣的问题(对我来说确实如此),我有一个查询,在这个查询中我有一个列表行:Python 2.7 ndb put()工作,但在15-30秒后不会同时更新结果,python-2.7,google-app-engine,google-cloud-datastore,app-engine-ndb,Python 2.7,Google App Engine,Google Cloud Datastore,App Engine Ndb,我有一个非常有趣的问题(对我来说确实如此),我有一个查询,在这个查询中我有一个列表行: class TestList(ndb.Model): testing_list = ndb.StringProperty(repeated=True) list_id = ndb.IntegerProperty() 此外,我还提供了API方法,用于根据补丁请求更改测试列表。 代码: @app.route('/list/change', methods=['PATCH']) def list_
class TestList(ndb.Model):
testing_list = ndb.StringProperty(repeated=True)
list_id = ndb.IntegerProperty()
此外,我还提供了API方法,用于根据补丁请求更改测试列表。
代码:
@app.route('/list/change', methods=['PATCH'])
def list_change():
list_id = request.form['id']
list_elements = request.form['elements']
query = TestList.query(TestList.list_id == int(list_id))
try:
query.fetch()[0].list_id
except IndexError:
return 'Error', 400
new_elements = str(list_elements).replace(' ', '').split(',')
query.fetch()[0].testing_list = [element for element in new_elements if element in query.fetch()[0].testing_list]
query.fetch()[0].put()
testing_list_extend(query.get(), new_elements)
return 'Success', 200
@ndb.transactional
def testing_list_extend(list_key, new_elements):
for element in new_elements:
query = TestList.query(ancestor=list_key.key)
if element not in query.fetch()[0].testing_list:
query.fetch()[0].testing_list.append(element)
query.fetch()[0].put()
return '200'
输入时,我得到类似“Element1,Element2”的字符串,这是body request中的元素,而id类似于“1”。所以在我解析字符串并制作列表之后。在我想要在测试列表中添加新的唯一元素之后。在这一部分,我有一个错误:有时,当我添加新元素并通过get请求得到测试列表时,我得到的是空列表,但有15-30秒的时间,我得到了一个我想在不久前看到的列表。
例如,在bodyPATCH请求中:
id = '1'
elements = 'Element1, Element2'
通过获取测试列表,我正在等待什么响应:
[Element1, Element2]
我经常得到的是:
[Element1, Element2]
我得到的东西非常罕见(我认为是虫子):
这里有一些错误,但我认为问题的原因是您的多次卖出和卖出。如果您可以使用
TestList
s键而不是TestList.list\u id
,这会更好。这样,您的函数将如下所示:
@app.route('/list/change', methods=['PATCH'])
def list_change():
list_id = request.form['id']
list_elements = request.form['elements']
new_elements = str(list_elements).replace(' ', '').split(',')
try:
testing_list_extend(ndb.Key(TestList, long(list_id)), new_elements)
return 'Success', 200
except Exception as e:
return e.message, 400
@ndb.transactional
def testing_list_extend(list_key, new_elements):
test_list = list_key.get()
if test_list is None:
raise Exception('Test List ID does not exist')
l = []
l.extend(entity.testing_list) # the existing list
l.extend(new_elements) # the append the new_elements
entity.testing_list = list(set(l)) # remove duplicates
entity.put()
@app.route('/list/change', methods=['PATCH'])
def list_change():
list_id = request.form['id']
list_elements = request.form['elements']
new_elements = str(list_elements).replace(' ', '').split(',')
try:
# Only return the Key, to be used in the transaction below
query = TestList.query(TestList.list_id == int(list_id)).fetch(2, keys_only=True)
if len(query) == 0:
raise Exception("Found no 'TestList' with list_id == %s" % list_id)
# Double check for duplicates
elif len(query) == 2:
raise Exception("Found more than one 'TestList' with list_id == %s" % list_id)
testing_list_extend(query[0], new_elements)
return 'Success', 200
except Exception as e:
return e.message, 400
@ndb.transactional
def testing_list_extend(list_key, new_elements): # same
test_list = list_key.get()
if test_list is None:
raise Exception('Test List ID does not exist')
l = []
l.extend(entity.testing_list) # the existing list
l.extend(new_elements) # the append the new_elements
entity.testing_list = list(set(l)) # remove duplicates
entity.put()
否则,请尝试这样做:
@app.route('/list/change', methods=['PATCH'])
def list_change():
list_id = request.form['id']
list_elements = request.form['elements']
new_elements = str(list_elements).replace(' ', '').split(',')
try:
testing_list_extend(ndb.Key(TestList, long(list_id)), new_elements)
return 'Success', 200
except Exception as e:
return e.message, 400
@ndb.transactional
def testing_list_extend(list_key, new_elements):
test_list = list_key.get()
if test_list is None:
raise Exception('Test List ID does not exist')
l = []
l.extend(entity.testing_list) # the existing list
l.extend(new_elements) # the append the new_elements
entity.testing_list = list(set(l)) # remove duplicates
entity.put()
@app.route('/list/change', methods=['PATCH'])
def list_change():
list_id = request.form['id']
list_elements = request.form['elements']
new_elements = str(list_elements).replace(' ', '').split(',')
try:
# Only return the Key, to be used in the transaction below
query = TestList.query(TestList.list_id == int(list_id)).fetch(2, keys_only=True)
if len(query) == 0:
raise Exception("Found no 'TestList' with list_id == %s" % list_id)
# Double check for duplicates
elif len(query) == 2:
raise Exception("Found more than one 'TestList' with list_id == %s" % list_id)
testing_list_extend(query[0], new_elements)
return 'Success', 200
except Exception as e:
return e.message, 400
@ndb.transactional
def testing_list_extend(list_key, new_elements): # same
test_list = list_key.get()
if test_list is None:
raise Exception('Test List ID does not exist')
l = []
l.extend(entity.testing_list) # the existing list
l.extend(new_elements) # the append the new_elements
entity.testing_list = list(set(l)) # remove duplicates
entity.put()
问题出在ndb缓存中
为此问题编写功能测试,发现google.cloud中数据存储上的行已更新,但同时执行了GET请求并获得了旧日期,因此在GET方法中放置了ndb.GET_context().clear_cache(),效果良好 谢谢,我修改了我的代码!但问题在于ndb缓存。我为此问题编写了功能测试,发现google.cloud数据存储中的行已更新,但同时我确实收到了请求并获得了旧日期,因此在我的GET方法中,我将
ndb.GET\u context().clear\u cache()
放入其中,效果很好。@Ruslan为了社区的利益,您能将此作为一个答案发布吗?