首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >返回空上下文的Google python map-reduce库

返回空上下文的Google python map-reduce库
EN

Stack Overflow用户
提问于 2013-05-25 00:40:25
回答 2查看 371关注 0票数 3
代码语言:javascript
复制
class DeleteOldObservationsMapper(object):
  """Mapper for deleting old observations."""

  def __init__(self):
      logging.info('DeleteOldObservationsMapper init')
      ctx = mapreduce.context.get()
      when = ctx.mapreduce_spec.mapper.params.get('before_timestamp_seconds')
      assert when
      self.before_datetime = datetime.datetime.utcfromtimestamp(when)
      logging.info('before_datetime %s', self.before_datetime)

以下是错误跟踪:

代码语言:javascript
复制
ERROR    2013-05-24 16:03:38,662 webapp2.py:1552] 'NoneType' object has no attribute 
'mapreduce_spec'
Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/Users/leoromanovsky/code/adapt/server/mapreduce/base_handler.py", line 66, in post
    self.handle()
  File "/Users/leoromanovsky/code/adapt/server/mapreduce/handlers.py", line 320, in handle
    tstate = model.TransientShardState.from_request(self.request)
  File "/Users/leoromanovsky/code/adapt/server/mapreduce/model.py", line 993, in from_request
    handler = mapreduce_spec.mapper.handler
  File "/Users/leoromanovsky/code/adapt/server/mapreduce/model.py", line 618, in get_handler
    return util.handler_for_name(self.handler_spec)
  File "/Users/leoromanovsky/code/adapt/server/mapreduce/util.py", line 149, in handler_for_name
    return getattr(resolved_name.im_class(), resolved_name.__name__)
  File "/Users/leoromanovsky/code/adapt/server/jobs.py", line 22, in __init__
    when = ctx.mapreduce_spec.mapper.params.get('before_timestamp_seconds')
AttributeError: 'NoneType' object has no attribute 'mapreduce_spec'
EN

回答 2

Stack Overflow用户

发布于 2013-11-29 02:53:16

我们最近在一个threadsafe: false项目中遇到了这个问题,并且能够通过改变导入context模块的方式来解决这个问题。凯文在this bug report中很好地解释了这个问题。

kevin.mo...@gmail.com也许这会对其他人有所帮助,但我看到了这个问题,并为我的用例解决了这个问题。在我们的项目中,mapreduce库位于子文件夹"libs/external/mapreduce“中,而不是项目的根目录。

该库将上下文(以及其他内容)作为from mapreduce import context从自身导入。因此,为了让它工作,我们使用导入操作,如下所示:

导入os,sys sys.path.append(os.path.join(os.path.dirname(__file__),‘libs/external’)

然而,在我们代码中的一些地方,我们仍然像这样导入上下文:

从libs.external.mapreduce导入上下文

这实际上会导致context被导入两次,mapreduce.context.Context._local将被设置两次,从而拥有两个不同的thread._local实例。当调用Context._set以在映射器函数中存储上下文以供以后检索时,它将使用原始的thread._local实例。

稍后,当我们的映射器模块再次导入上下文时,它将获得一个新的thread._local实例。当我们使用calling context.get()时,它返回的是实际上没有上下文的新实例:

AttributeError:“NoneType”对象没有特性“”mapreduce_spec“”

将我们所有的导入切换到from mapreduce import context为我们解决了这个问题。

票数 2
EN

Stack Overflow用户

发布于 2013-07-02 04:40:08

我想这就是你的问题的答案:

http://code.google.com/p/appengine-mapreduce/issues/detail?id=127

mapreduce.context.get()根本不是线程保存...

所以你可以做的就是把它打包到一个包装器中,使它成为线程保存,然后使用Lock机制。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16739802

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档