【问题标题】:Can't run deferred tasks无法运行延迟任务
【发布时间】:2014-07-19 20:57:23
【问题描述】:

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

                    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)

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

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。请问有什么问题吗?

【问题讨论】:

  • 该任务无法加载您的腌制参数(jsonified_task_records)。所以搜索:“app engine task pickle keyerror”和你的jsonified_task_records
  • 感谢您的提示。我找到了这个,但它说使用我已经在使用的延迟库。 stackoverflow.com/questions/4836697/…

标签: python google-app-engine


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-14
    • 2018-10-21
    • 1970-01-01
    • 2013-11-11
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 2016-12-25
    相关资源
    最近更新 更多