Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 757
Alexander Putilin Avatar asked Aug 24 '14 12:08

Alexander Putilin


1 Answers

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 .

like image 90
Victor Shelepen Avatar answered Nov 05 '22 08:11

Victor Shelepen