schollii / pypubsub

A Python publish-subcribe library (moved here from SourceForge.net where I had it for many years)
189 stars 29 forks source link

Pause messages of type #51

Closed paddymccrudden closed 3 years ago

paddymccrudden commented 3 years ago

Hi @schollii. I am interested to know whether it is possible to put a stop on messages being sent for a while. The idea is a follows:

What I would like is that I prevent A from sending the messages y(B), Y(C), ... etc. until the "last" message is received from H.

The kind of thing I am imagining is:

with pub.withhold("Z", kwargs["key1"] == "AA", kwargs["key2"] == "BB"):
    pub.sendMessage(x)

pub.sendMessage("Z", **kwargs)

The reason for considering this idea is that each of the messages y generates redundant activity so that it needs only be sent once.

I understand that this process may go against the spirit of asynchronous messaging, but I feel it is an interesting pattern.

schollii commented 3 years ago

The main problem is that in a pub/sub architecture there is no builtin way for A to know which components receive the message, and even less so when it has received all the messages. The beauty of pubsub is decoupling, asynchronicity and fire-and-forget. What you are trying to do between A and recipients introduces a lot of coupling between the components, goes against pubsub. What you are describing is more like a message queue, but that's not even quite right.

So you need to revisit whether pubsub is the right architecture for your app, and if it is then you need to take a step back and challenge your idea that your approach is the only way, because it breaks pubsub design. You need to think about how you can design your messaging without these feedback loops. Your design will gain a lot from this.

paddymccrudden commented 3 years ago

Thanks for the prompt reply! I did expect your answer, and know you are right. I will make some changes to handle these types of cases.

Note, however, in the suggestion above pub.withhold, it doens't have to know about A at all. It only needs to know what messages to stop publishing. But to be clear, I agree that my suggestion is against the spirit of such messaging.

schollii commented 3 years ago

OK cool I hope you find a good solution. I will close this issue.