Python信号与pubsub有何不同?

2024-04-29 09:45:23 发布

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

Django和Flask使用信号,后者使用Blinker库。在Python、Blinker和Python pubsub库的上下文中,信号和pubsub如何比较?我什么时候用这个呢?在


Tags: djangoflask信号pubsubblinker
2条回答

Blinker docs和{a2}。在

Blinker和{}而言,它们是一样的。不同之处在于他们如何去做:

使用Blinker当您订阅一个信号时,您给出信号的名称,当您激活该信号时,您将传递激活对象。在

使用PubSub订阅侦听器时,您将给出名称(与Blinker相同),但当您通知侦听器时,您直接将数据作为关键字参数传递。由于传递数据的关键字参数方法,可以使用PubSub进行更多的安全检查。在

就个人而言,我会选择Signals,因为它更符合我的思维方式,但是{}当然也有一席之地。在

这可能会明确Pubsub与信号的关系:http://pubsub.sourceforge.net/apidocs/concepts.html

Pubsub facilitates the decoupling of components (callables, modules, packages) within an application. It does this by:

  • Allowing parts of the application to send messages to “the rest of the application” without having to know
    • if the messages will be handled:
      • perhaps the message will be ignored completely,
      • or handled by a many different parts of the application;
    • how the messages will be handled:
      • what will be done with the message and its contents;
      • in what order any given message will be sent to the rest of the application;
  • Allowing parts of the application to receive and handle messages from “the rest of the application” without having to know who sent the messages.

A listener is “a part of the application that wants to receive messages”. A listener subscribes to one or more topics. A sender is any part of the application that asks Pubsub to send a message of a given topic. The sender provides data, if any. Pubsub will send the message, including any data, to all listeners of the message’s topic.

相关问题 更多 >