2011-01-22 85 views
5

有没有办法在执行任务时处理任何软期限? DeadlineExceededError在执行10分钟后抛出,我在几秒钟后给出一些东西。 我想在任务死亡之前清理一些东西并创建一个新任务。这可能需要几秒钟。有没有办法通过捕获任何异常,如约9分钟来做到这一点。 我知道我可以在9分钟后手动抛出异常。但是这可以由GAE自动完成吗?Google App Engine任务截止日期

class FillMtxHandler(): 

def post(self,index,user,seqlen): 

    try :   
     FillMtx(index,user,seqlen) 

    except DeadlineExceededError: 

     deferred.defer(self.post,index,user,seqlen) 

以上是我的代码。索引是一个列表,从0开始。它将在FillMtx中递增。一旦截止日期超出错误被抛出,我想继续索引最后增加的位置。我收到以下错误

The API call taskqueue.BulkAdd() was explicitly cancelled. 
Traceback (most recent call last): 
    File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/__init__.py", line 517, in __call__ 
    handler.post(*groups) 
    File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/deferred/deferred.py", line 258, in post 
    run(self.request.body) 
    File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/deferred/deferred.py", line 124, in run 
    return func(*args, **kwds) 
    File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/deferred/deferred.py", line 166, in invoke_member 
    return getattr(obj, membername)(*args, **kwargs) 
    File "/base/data/home/apps/0xxopdp/3.347813391084738922/fillmtx.py", line 204, in post 
    deferred.defer(self.post,index,user,seqlen) 
    File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/deferred/deferred.py", line 241, in defer 
    return task.add(queue, transactional=transactional) 
    File "/base/python_runtime/python_lib/versions/1/google/appengine/api/taskqueue/taskqueue.py", line 688, in add 
    return Queue(queue_name).add(self, transactional=transactional) 
    File "/base/python_runtime/python_lib/versions/1/google/appengine/api/taskqueue/taskqueue.py", line 744, in add 
    self.__AddTasks(tasks, transactional) 
    File "/base/python_runtime/python_lib/versions/1/google/appengine/api/taskqueue/taskqueue.py", line 770, in __AddTasks 
    apiproxy_stub_map.MakeSyncCall('taskqueue', 'BulkAdd', request, response) 
    File "/base/python_runtime/python_lib/versions/1/google/appengine/api/apiproxy_stub_map.py", line 86, in MakeSyncCall 
    return stubmap.MakeSyncCall(service, call, request, response) 
    File "/base/python_runtime/python_lib/versions/1/google/appengine/api/apiproxy_stub_map.py", line 286, in MakeSyncCall 
    rpc.CheckSuccess() 
    File "/base/python_runtime/python_lib/versions/1/google/appengine/api/apiproxy_rpc.py", line 126, in CheckSuccess 
    raise self.exception 
CancelledError: The API call taskqueue.BulkAdd() was explicitly cancelled. 

我发现一个新任务已创建并排队。但为什么GAE仍然会抛出这个错误?

回答

4

您无法控制何时获得软限期超时错误。相反,您应该使用自己的计时器(在开始时以壁挂时间为准,并将其与主循环周围每次旅行的当前时间进行比较),并在距离您要停止的极限附近时放弃。

4

9分钟后,您不必提出异常;当软期限异常提高时,您有足够的时间通过deferred清理任务添加到Task Queue

from google.appengine.ext import deferred 
... 
try: 
    # Do your stuff 
except DeadlineExceededError: 
    deferred.defer(do_your_cleanup, ..) 

以这种方式,您有10分钟的时间来完成您的应用程序中所需的任何清理工作。

+0

我该如何确定在命中硬限制之前将任务添加到任务队列?我得到了“CancelledError:明确取消了API调用taskqueue.BulkAdd()”,即使已经添加了任务 – Sam 2011-01-22 15:00:29

+0

如果用完宽限时间,CancelledError会启动。 – systempuntoout 2011-01-22 17:37:44