Flask signals: why is it not ok to modify data on signal?
Asked Answered
R

1

8

Flask documentations says:

Also keep in mind that signals are intended to notify subscribers and should not encourage subscribers to modify data

I am wondering, why is so?

I'm using Flask-User library, and I would like to set some default fields for user when a user registers(e.g. set displayname to be equal to username) and then update the db. Flask-User sends user_registered signal when user registers. Why is it a bad idea to subscribe to the signal, and update db in it?

Recapitulation answered 24/8, 2014 at 12:53 Comment(2)
I think that sentence is referring to not modify the signal data, not any other data (like the database, etc).Starnes
This is a good question. Having read through the blinker documentation I don't see anything that talks about data modification.Confirm
P
3

It is the over-round solution. I guess I am strong Drupal/PHP developer. Up to the 7-th version everything was built using hooks - signals(Flask). The project becomes a mess when everything is built on hooks. It is a quick process but dangerous. Signals are designed to be used as the observer pattern, It likes to events. It is the main idea. But when we able to update the context, subjects. It likes to the chain responsibility. The main problem is the chain. So if one item fails the anothers receive the wrong state. It is the main problem. Sometimes it is difficult to find a culprit who fails. Because we have different signals from different subjects these modify the common scope.

You can extend almost everywhere but you should not change data. To extend right you should use clean OOP solutions.

Sometimes we need to change some logic. So we need define a basket what we need change. In Drupal 8 we use Dependency Injection Containers - Services. Services are described in separated files. So we able changes relations and injections.

There is the interesting library - https://pypi.python.org/pypi/Flask-Injector .

Pedersen answered 30/8, 2014 at 9:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.