在Python中使用任务队列进行Google App Engine的异步调用
我刚接触谷歌应用引擎中的任务队列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()
谢谢
3 个回答
0
所以你可以使用:
add_async(task, transactional=False, rpc=None)
来源:https://developers.google.com/appengine/docs/python/taskqueue/queues
你需要去上面的链接阅读文档,然后把里面的内容应用到你自己的代码中。
0
在App Engine上,任务队列是异步的;也就是说,添加任务的请求并不知道这个任务什么时候会被执行(除非你进行远程调用或者其他特意的沟通)。你可能注意到的情况是开发环境中的单线程特性;但在实际生产环境中情况就不一样了。
2
最近开发的 dev_appserver2 让用户请求和任务队列请求可以同时进行,这样能更真实地模拟实际生产环境。