DanielSank / observed

Observer pattern in python
MIT License
33 stars 4 forks source link

Make the sequence order of callbacks to observers of the same event predictable #27

Closed andreas-0815-qwertz closed 1 year ago

andreas-0815-qwertz commented 1 year ago

As observers are stored in a plain dict the order of callbacks is only predictable since Python 3.7, i.e. it will call back in the order of calls to add_observer. It would be nice to have an more explicit way to specify the order of callbacks in general, and specifically for older Python versions. The latter case is unimportant for me personally though.

DanielSank commented 1 year ago

Hi @andreas-0815-qwertz. I am hesitant to make callback order part of the contract with this library. What is your use case?

andreas-0815-qwertz commented 1 year ago

I'll try to provide a rationale below. For a prominent example, of what I am after, in another language, please look at the Boost Signal2 tutorial.

My use case is a bit entangled: I am building a testbed for an application driven by a cyclic executive. To keep dependencies under control, I have made the 100ms cyclic call observable. One of the observers is a timer provider that can be used by other observers. Obviously, I want to make sure that the timer provider is called before other observers that might use its services.

As long as the execution of my whole application is under my own control, I can ensure such sequencing manually. Managing such dependencies can become complicated to impossible, if:

I hope this makes sense to you :-) ...

DanielSank commented 1 year ago

Hey @andreas-0815-qwertz. I am going to leave callback ordering out of observed for the time being. I am not a good enough programmer to know whether adding that feature would cause more trouble than it's worth. If we do add it, and it turns out to be a maintenance burden, then we can't undo it without breaking backwards compatibility.