金字塔中请求外的自定义事件

时间:2015-08-23 12:51:18

标签: python pyramid

我正在尝试在我的应用程序的__init__方法中通知我的自定义事件,并在应用程序的包含部分中捕获/订阅。但由于某种原因,导入代码中的订阅者未被调用。如果在同一导入的代码中我订阅了Pyramid事件,一切正常。这是代码示例。

__初始化__。PY

class MyEvent(object): pass

def main(...):
   ...
   config.include('some_module')
   config.registry.notify(MyEvent())
   ...

some_module.py

def handle_event(e):
  print 'event', e

def includeme(config):
  print 'module included'
  config.add_subscriber(handle_event, 'myapp.MyEvent')

打印行module included,但不打印event行。请问金字塔中的notify / subscribe是如何工作的?代码中某处有错误吗?感谢

1 个答案:

答案 0 :(得分:0)

config.include()之后,您必须使用Configurator显式提交config.commit(),然后才会将config.add_subscriber(...)添加到内部注册表中以供使用。

换句话说,目前当您致电.notify(...)时,尚未注册该活动的订阅者。