Python - How are signals different from pubsub?


Question

Django and Flask make use of signals — the latter uses the Blinker library. In the context of Python, Blinker and the Python pubsub library, how do signals and pubsub compare? When would I use one or the other?

1
29
3/4/2011 8:33:45 PM

Accepted Answer

Blinker docs and PubSub docs.

As far as Blinker and PubSub go, they are the same thing. The difference is in how they go about it:

With Blinker when you subscribe to a signal you give the name of the signal, and when you activate the signal you pass the activating object.

With PubSub when you subscribe to a listener you give the name (same as Blinker), but when you notify the listener you pass the data directly as keyword arguments. Because of the keyword argument method of passing data it is possible to have many more safety checks using PubSub.

Personally, I would go with Signals as it matches my way of thinking better, but PubSub certainly has a place also.

15
8/31/2015 3:06:10 PM

This might clear up exactly how Pubsub relates to signals: 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.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon