How are signals different from pubsub?
Asked Answered
T

2

30

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?

Thunderclap answered 4/3, 2011 at 20:33 Comment(1)
I'm kind of looking for a definitive answer from someone who's been down this path such as, "They're the same thing" or "PubSub usually involves X while signals usually mean Y."Thunderclap
D
16

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 Blinker as it matches my way of thinking better, but PubSub certainly has a place also.

Dice answered 11/2, 2013 at 23:30 Comment(0)
R
6

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.

Rubinrubina answered 7/3, 2011 at 1:30 Comment(1)
Don't all these things apply to signals and receivers too?Samuels

© 2022 - 2024 — McMap. All rights reserved.