返回内容后处理数据-GAE Python
我想收集一些需要一些时间来处理的统计数据,但这不会影响正在返回的用户内容。我目前的做法是先缓存,然后用cron作业处理它 有没有办法在返回用户内容后立即执行此操作 应该是这样的:返回内容后处理数据-GAE Python,python,google-app-engine,python-2.7,Python,Google App Engine,Python 2.7,我想收集一些需要一些时间来处理的统计数据,但这不会影响正在返回的用户内容。我目前的做法是先缓存,然后用cron作业处理它 有没有办法在返回用户内容后立即执行此操作 应该是这样的: def post(self): self.response.out.write("some output") # here user should not wait any more output # loading should be end in user browser. col
def post(self):
self.response.out.write("some output")
# here user should not wait any more output
# loading should be end in user browser.
collectSomeStatistics(self)
(我正在使用Python 2.7)您可以使用
因此,您的代码变成:
def post(self):
deferred.defer(collectSomeStatistics,args)
self.response.out.write("some output")
然后,它将作为一个单独的任务执行您的函数,对deferred的调用将立即返回。当然,您将无法在返回给用户的内容中包含延迟函数的任何结果 您可以使用
因此,您的代码变成:
def post(self):
deferred.defer(collectSomeStatistics,args)
self.response.out.write("some output")
然后,它将作为一个单独的任务执行您的函数,对deferred的调用将立即返回。当然,您将无法在返回给用户的内容中包含延迟函数的任何结果 googleappengine的API提供了一个处理此类情况的方法
from google.appengine.api import taskqueue
def post(self):
taskqueue.add(queue_name='expensive_query', url="/path/to/_an_internal_method/")
self.response.out.write("some output")
谷歌应用程序引擎的API提供了一个处理此类情况的平台
from google.appengine.api import taskqueue
def post(self):
taskqueue.add(queue_name='expensive_query', url="/path/to/_an_internal_method/")
self.response.out.write("some output")
谢谢,这似乎是一个简单而伟大的工具来完成一般任务,但出于我的目的,我需要将一些对象传递给函数。因此,我认为@Paul的答案更适合于这个特定的目的。谢谢,这似乎是一个简单而伟大的工具来完成一般任务,但出于我的目的,我需要将一些对象传递给函数。所以@Paul的答案更适合这个特定的目的,我想。你甚至可以将Paul的答案与Channel API中的一些功能结合起来,这样你就可以在收集统计数据后通知页面。你让我很高兴在设置taskqueue之前我用谷歌搜索了这一点:)你甚至可以将Paul的答案与Channel API中的一些功能结合起来,这样你就可以在完成任务后通知页面已经收集了统计数据。你让我很高兴在设置taskqueue之前我在谷歌上搜索了这个:)