2011-01-22 82 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