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

2024-09-27 07:34:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在应用程序的__init__方法中通知我的自定义事件,并在应用程序的包含部分捕获它/订阅它。但由于某些原因,导入代码中的订户没有被调用。如果在同一个导入的代码中,我订阅了金字塔事件,那么一切都正常。下面是代码示例。你知道吗

\uuu init\uuuuuuuuuy.py

class MyEvent(object): pass

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

一些_模块.py

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

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

打印行module included,但不打印event行。金字塔中的通知/订阅就是这样工作的吗?代码中有错误吗?谢谢


Tags: 方法代码pyeventconfig应用程序initdef
1条回答
网友
1楼 · 发布于 2024-09-27 07:34:36

config.include()之后,必须使用config.commit()显式提交Configurator,然后config.add_subscriber(...)才能添加到内部注册表中使用。你知道吗

换句话说,当前当您调用.notify(...)时,还没有注册事件的订户。你知道吗

相关问题 更多 >

    热门问题