无法运行延期任务

时间:2014-07-19 20:57:24

标签: python google-app-engine

这就是我在任务队列上添加任务的方式

                    taskqueue.Task(url='/worker',
                               params={"json_records": jsonified_task_records,
                                       "user": user.key.urlsafe()}
                               ).add(queue_name='postios')

然后在单元测试中,我运行将任务推送到任务队列中的视图。现在我想执行实际任务:

rv = self.client.post('api/v1.0/ftrecords/device_id/123', headers=headers, data=json.dumps(records))
            # Get the task out of the queue
            tasks = self.taskqueue_stub.get_filtered_tasks()
            self.assertEqual(1, len(tasks))
            # Run the task
            task = tasks[0]
            deferred.run(task.payload)

然而,这会在延迟的lib中引发异常:

def run(data):
  """Unpickles and executes a task.

  Args:
    data: A pickled tuple of (function, args, kwargs) to execute.
  Returns:
    The return value of the function invocation.
  """
  try:
    func, args, kwds = pickle.loads(data)
  except Exception, e:
    raise PermanentTaskFailure(e)
  else:
    return func(*args, **kwds)

我在' pickle.loads()'周围注释了一个例外。获得更好的堆栈跟踪:

Traceback (most recent call last):
  File "/Users/hooman/workspace/F11A/src/tests/test_rest_records.py", line 451, in test_post_updated_records_new_timestamp
    deferred.run(task.payload)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/deferred/deferred.py", line 142, in run
    func, args, kwds = pickle.loads(data)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 1382, in loads
    return Unpickler(file).load()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 858, in load
    dispatch[key](self)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 1161, in load_long_binget
    self.append(self.memo[repr(i)])
KeyError: '1601073011'

我正在运行最新的1.9.7。知道什么可能是错的吗?

1 个答案:

答案 0 :(得分:0)

延迟包是直接向队列添加任务的替代方法,但是您将它们混合在一起--deferred.run期望处理使用deferred.defer添加的内容,因此要么使用它来添加任务,要么让你的测试直接运行你的/工作者URL。