如何在 Google App Engine 上使用任务队列获取返回值(如 Ajax)

发布于 2024-09-30 01:58:09 字数 1786 浏览 4 评论 0原文

我可以使用任务队列来更改数据库值,但是如何使用任务队列像Ajax一样获取返回值?

这是我的代码:

from google.appengine.api.labs import taskqueue
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
import os

class Counter(db.Model):
    count = db.IntegerProperty(indexed=False)

class BaseRequestHandler(webapp.RequestHandler):
    def render_template(self, filename, template_values={}):
        values={
        }
        template_values.update(values)
        path = os.path.join(os.path.dirname(__file__), 'templates', filename)
        self.response.out.write(template.render(path, template_values))


class CounterHandler(BaseRequestHandler):
    def get(self):
        self.render_template('counters.html',{'counters': Counter.all()})

    def post(self):
        key = self.request.get('key')
        # Add the task to the default queue.
        for loop in range(0,1):
            a=taskqueue.add(url='/worker', params={'key': key})

        #self.redirect('/')

        self.response.out.write(a)

class CounterWorker(webapp.RequestHandler):
    def post(self): # should run at most 1/s
        key = self.request.get('key')
        def txn():
            counter = Counter.get_by_key_name(key)
            if counter is None:
                counter = Counter(key_name=key, count=1)
            else:
                counter.count += 1
            counter.put()
        db.run_in_transaction(txn)
        self.response.out.write('sss')#used for get by task queue

def main():
    run_wsgi_app(webapp.WSGIApplication([
        ('/', CounterHandler),
        ('/worker', CounterWorker),
    ]))

if __name__ == '__main__':
    main()

如何显示“sss”?

I can use a task queue to change the database value, but how can I get the return value like Ajax using task queue?

This is my code:

from google.appengine.api.labs import taskqueue
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
import os

class Counter(db.Model):
    count = db.IntegerProperty(indexed=False)

class BaseRequestHandler(webapp.RequestHandler):
    def render_template(self, filename, template_values={}):
        values={
        }
        template_values.update(values)
        path = os.path.join(os.path.dirname(__file__), 'templates', filename)
        self.response.out.write(template.render(path, template_values))


class CounterHandler(BaseRequestHandler):
    def get(self):
        self.render_template('counters.html',{'counters': Counter.all()})

    def post(self):
        key = self.request.get('key')
        # Add the task to the default queue.
        for loop in range(0,1):
            a=taskqueue.add(url='/worker', params={'key': key})

        #self.redirect('/')

        self.response.out.write(a)

class CounterWorker(webapp.RequestHandler):
    def post(self): # should run at most 1/s
        key = self.request.get('key')
        def txn():
            counter = Counter.get_by_key_name(key)
            if counter is None:
                counter = Counter(key_name=key, count=1)
            else:
                counter.count += 1
            counter.put()
        db.run_in_transaction(txn)
        self.response.out.write('sss')#used for get by task queue

def main():
    run_wsgi_app(webapp.WSGIApplication([
        ('/', CounterHandler),
        ('/worker', CounterWorker),
    ]))

if __name__ == '__main__':
    main()

How can I show the 'sss'?

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

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

发布评论

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

评论(1

二智少女猫性小仙女 2024-10-07 01:58:09

当前的任务队列 API 不支持处理返回值或将他们送回原点。您的应用程序引擎进程的寿命不够长,无法使该编程范例发挥作用。

在您的示例中,看起来您想要的是这样的:

  1. 创建任务
  2. 返回将轮询任务状态处理程序的 AJAX 代码
  3. 任务处理,使用返回值更新数据存储
  4. 任务状态 url 返回更新的值

或者,如果您不这样做如果不想将“sss”返回给客户端,而是需要它进行进一步处理,则需要将方法分成多个部分。第一部分创建任务然后退出。在任务过程结束时,它添加一个新任务本身,以使用返回值回调到第二部分。

The current Task Queue API doesn't support processing return values or sending them back to the point of origin. Your appengine process isn't long-lived enough for that programming paradigm to work.

In your example, it looks like what you want is something like this:

  1. Create task
  2. Return AJAX code that will poll a task-status handler
  3. Task processes, updates datastore with a return value
  4. Task-status url returns updated value

Alternatively, if you don't want to return the 'sss' to the client but instead need it for further processing, you'll need to split your method into multiple parts. The first part creates the task and then exits. At the end of the task's process, it adds a new task itself to call back into the second part with the return value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文