isinstance()因未知原因返回False

时间:2016-01-13 15:25:06

标签: python google-app-engine

所以,情况如下:
我使用GAE Pipeline lib作为流程,我的类FanoutPipeline来自库中的Pipeline类:

from pipeline import pipeline
class FanoutPipeline(pipeline.Pipeline):

当管道进程运行时,它会检查以验证该对象实际上是从Pipeline派生的类的实例,但它返回False:

  yielded = pipeline_iter.send(next_value)
  # here I check that yielded is an object of `FanoutPipeline`
  # class, plz see logs output below
  if isinstance(yielded, Pipeline):  # returns false here

我添加了大量日志来获取类树及其模块:

  import inspect
  logging.debug('Yielded: {}'.format(yielded))
  for cls in inspect.getmro(yielded.__class__):
      logging.debug('Yielded base: {}'.format(inspect.getmodule(cls)))
      logging.debug('Yielded base: {}'.format(cls))
  logging.debug('PipelineMeta classes: {}'.format(_PipelineMeta._all_classes))
  logging.debug('Pipeline: {}'.format(Pipeline))
  logging.debug('Pipeline module: {}'.format(inspect.getmodule(Pipeline)))
  for cls in inspect.getmro(Pipeline):
    logging.debug(inspect.getmodule(cls))
  if isinstance(yielded, Pipeline):

得到了输出:

D 21:55:48.079 Yielded: project.handlers.pipeline_gcm.FanoutPipeline(*(None, {u'campaign_id': u'xxx', u'campaign_name': u'xmas notification', u'execution': {u'action': u'market', u'conditions': {u'delayBetweenNotificationsHours': 0, u'in... (515 bytes), **{})
D 21:55:48.079 Yielded base: <module 'project.handlers.pipeline_gcm' from '/base/data/home/apps/s~project-dev3/9.389913797024223872/projects/handlers/pipeline_gcm.pyc'>
D 21:55:48.079 Yielded base: <class 'project.handlers.pipeline_gcm.FanoutPipeline'>
D 21:55:48.080 Yielded base: <module 'pipeline.pipeline' from '/base/data/home/apps/s~project-dev3/9.389913797024223872/libraries/pipeline/pipeline.pyc'>
D 21:55:48.080 Yielded base: <class 'pipeline.pipeline.Pipeline'>
D 21:55:48.080 Yielded base: <module '__builtin__' (built-in)>
D 21:55:48.080 Yielded base: <type 'object'>
D 21:55:48.081 PipelineMeta classes: [<class 'libraries.pipeline.pipeline.Pipeline'>]
D 21:55:48.081 Pipeline: <class 'libraries.pipeline.pipeline.Pipeline'>
D 21:55:48.081 Pipeline module: <module 'libraries.pipeline.pipeline' from '/base/data/home/apps/s~project-dev3/9.389913797024223872/libraries/pipeline/pipeline.pyc'>
D 21:55:48.082 <module 'libraries.pipeline.pipeline' from '/base/data/home/apps/s~project-dev3/9.389913797024223872/libraries/pipeline/pipeline.pyc'>
D 21:55:48.082 <module '__builtin__' (built-in)>

如您所见,在基类中已经明确pipeline.pipeline.Pipeline,模块路径匹配。

以下是一些可能发生的想法: 1.对象以某种方式在进程之间传递,并且检查失败,因为一个基类在一个进程中加载​​,另一个基类在另一个进程中加载​​。 2.相对路径存在差异,即创建对象的过程中的pipeline.pipeline模块 和libraries.pipeline.pipeline在其使用过程中进行isinstance检查。

你能帮我解决一下这个问题吗?我应该首先尝试什么?这可能是造成这种失败的原因吗?

1 个答案:

答案 0 :(得分:1)

您正在导入该类的两个版本,一个版本为libraries.pipeline.pipeline.Pipeline,另一个版本为pipeline.pipeline.Pipeline。这意味着您在/base/data/home/apps/s~project-dev3/9.389913797024223872/中同时拥有/base/data/home/apps/s~project-dev3/9.389913797024223872/librariessys.path。虽然你知道它们只是通往同一模块的两条路径,但python并不知道这一点并将它们视为两个不同的类。

您需要决定是要将导入基于libraries还是libraries内的各种模块,然后一致地编写导入。你也应该停止添加&#34; unchosen&#34;目录到sys.path,以便以错误的方式导入模块,失败。