【问题标题】:Asynchronous call in google appengine using task queues in python使用python中的任务队列在google appengine中进行异步调用
【发布时间】:2011-09-07 11:46:56
【问题描述】:

我是谷歌应用引擎中任务队列 API 的新手。我创建了一个新队列并使用 taskqueue.add() 函数在其中添加了一个任务。我已经定义了任务的 url,并写下了任务 url 的逻辑。但是该任务不会异步发生,因为应用程序正在等待任务完成,然后在 taskqueue.add() 函数之后继续执行语句。如何使任务异步?对此问题的任何帮助表示赞赏。

代码如下所示

class botinitiate(webapp.RequestHandler):
    def get(self):
        # some more statements here
        template_values = {'token': token,
                           'me': user.user_id()
                          }

        taskqueue.add(url='/autobot', params={'key':game_key},queue_name='autobot')
        path = os.path.join(os.path.dirname(__file__), 'index.html')
        self.response.out.write(template.render(path, template_values))


class autobot(webapp.RequestHandler):
    def post(self):
        # task logic goes here


application = webapp.WSGIApplication([('/botinitiate',botinitiate),('/autobot',autobot)],debug=True)


def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

谢谢

【问题讨论】:

  • 您如何确认应用正在等待任务完成以发送响应?
  • 使用appengine的日志。它在继续执行 taskqueue.add() 之后的语句之前完成任务
  • 来自启动任务的处理程序和任务本身的示例代码会很有帮助。
  • 我已经在消息中添加了代码

标签: python google-app-engine task-queue


【解决方案1】:

最近开发的dev_appserver2 提供用户请求和任务队列请求之间的并发性,以更准确地模拟生产。

【讨论】:

    【解决方案2】:

    App Engine 上的任务队列是异步的;将任务排入队列的请求无法知道任务何时运行(缺少进行 RPC 调用或其他故意通信)。您可能会观察到 dev_appserver 开发环境的单线程特性;在生产中肯定不会出现这种情况。

    【讨论】:

      【解决方案3】:

      所以你会使用:

      add_async(task, transactional=False, rpc=None)
      

      来源:https://developers.google.com/appengine/docs/python/taskqueue/queues

      您需要阅读上述 URL 中的文档并将其应用到您自己的代码中。

      【讨论】:

        猜你喜欢
        • 2011-01-25
        • 2017-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多