使用 GAE 数据存储进行作业管理

发布于 2024-12-02 17:46:14 字数 514 浏览 2 评论 0原文

我的数据存储中有一个“计算”实体,该实体下有 700 万个对象。它具有以下属性(python 运行时):

class Calculation(db.Model):
    question = db.StringProperty(required=True)
    answer = db.StringProperty()

假设“question”属性的示例是“1+1”、“2+2”、“3+3”(不是很重要)。所有计算对象都以空答案属性开始。

当用户连接到应用程序时,会进行 ajax 调用,我的应用程序应该 [1] 获取具有空答案属性的 Calculation 对象并将其发送到用户的浏览器。然后,用户的浏览器评估问题并将其发送回不同的服务器处理程序。

如何更新特定计算对象的答案属性[2]?

如果有人能为我提供 [1] 和 [2] 的代码,那就太好了。对 App Engine 没有真正的经验,而且查询内容很混乱。如果我想尽可能节省服务器 CPU,最佳解决方案是什么?

谢谢!

I have a 'Calculation' entity in my datastore with say, 7 million objects under that entity. It has the following properties (python runtime):

class Calculation(db.Model):
    question = db.StringProperty(required=True)
    answer = db.StringProperty()

suppose examples of the 'question' property are things like '1+1', '2+2', '3+3' (not really important). All the calculation objects start out with empty answer properties.

When a user connects to the app, an ajax call is made and my app is supposed to [1] fetch a Calculation object with an empty answer property and send it to the user's browser. The user's browser then evaluates the question and sends it back to a different server handler.

How do I update that specific Calculation object's answer property[2]?

If someone could provide me with code for [1] and [2] that would be great. Not really experienced with App Engine and the query stuff is confusing. What is the best solution for this if I want to conserve as much server-CPU as possible?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

碍人泪离人颜 2024-12-09 17:46:14

我不知道我是否真正理解了。但您只需为第一个 ajax 调用返回实体计算的键和问题。当用户做出响应时,您首先通过键获取实体并更新属性answer

步骤 1,ajax 调用返回 JSON 格式的问题(例如):

# To fetch an empty answered question
qry = Calculation.All().filter('answer =', None)
ref = qry.get()

# The Json response
{ 'key': unicode(ref.key()),
  'question': ref.question}

步骤 2,通过键更新实体:

# key and answer are variable from an http GET or POST request.
ref = db.get(key)
ref.answer = answer
ref.put()

I don't know if i have really understand. But you simply need to return for your first ajax call the key of the entity Calculation and the question. When the user makes a response you firstly get the entity by the key and update the property answer.

Step 1, The ajax call return a question in JSON (for example):

# To fetch an empty answered question
qry = Calculation.All().filter('answer =', None)
ref = qry.get()

# The Json response
{ 'key': unicode(ref.key()),
  'question': ref.question}

Step 2, You update the entity by the key:

# key and answer are variable from an http GET or POST request.
ref = db.get(key)
ref.answer = answer
ref.put()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文