简体   繁体   English

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

[英]Custom events outside request in Pyramid

I'm trying to notify my custom event in __init__ method of my application, and catch it/subscribe in included part of application. 我正在尝试在我的应用程序的__init__方法中通知我的自定义事件,并在应用程序的包含部分中捕获它/ subscribe。 But for some reason the subscriber in imported code is not being called. 但由于某种原因,导入代码中的订阅者未被调用。 If in this same imported code I subscribe to Pyramid event everything works. 如果在同一导入的代码中我订阅了Pyramid事件,一切正常。 Here is the code example. 这是代码示例。

__init__.py __init__.py

class MyEvent(object): pass

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

some_module.py some_module.py

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

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

The line module included is printed, but not the event line. module included的行module included是打印的,但不是event行。 Is this how notify/subscribe in Pyramid is suppose to work? 请问金字塔中的notify / subscribe是如何工作的? Is there an error somewhere in the code? 代码中某处有错误吗? Thanks 谢谢

After you config.include() you will have to explicitly commit the Configurator using config.commit() before the config.add_subscriber(...) will have been added to the internal registry for use. config.include()之后,在将config.add_subscriber(...)添加到内部注册表以供使用之前,您必须使用config.commit()显式提交Configurator

In other words, currently when you call .notify(...) there are no subscribers for the event registered yet. 换句话说,目前当您调用.notify(...) ,尚未注册的事件的订阅者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM