When a Lino application starts up¶
This chapter describes what happens during site startup.
- site startup¶
The things that happen at the beginning of a Lino process.
- Lino process¶
There are four major phases in the life of a Lino process:
- application definition¶
The first part of the site startup when Django settings are being loaded. We see this as a separate phase because Lino does some magics there.
- data definition¶
The second part of the site startup when Django models are being loaded.
- site analysis¶
The third part of the site startup when Django models are loaded, and Lino analyzes them to add its own data structures.
When the site startup has finished and the actual process does what it is designed to do.
There are three
lino.api modules named after these phases:
A more detailed description follows.
manage.pyscript causes the module specified by
settings.pymodule) to be imported. This might happen twice (e.g. with
runserver). Everything in
Only now everything in
The Site startup phase¶
lino.core.plugins.Plugin.on_site_startup()on each plugin
Site.do_site_startup, which does the following:
lino.core.plugins.Plugin.before_analyze()on each plugin
lino.core.model.Model.on_analyze()on every model.
lino.core.model.Model.collect_virtual_fields()on every model. This attaches virtual fields to the model that declares them.
lino.core.plugins.Plugin.before_actors_discover()on each plugin.
Discover and initialize actors
lino.core.actors.Actor.after_site_setup()on each actor
lino.core.plugins.Plugin.on_ui_init()on each plugin
lino.core.plugins.Plugin.post_site_startup()on each plugin
A server startup signal for Django¶
This section is obsolete. It was for Django before 1.7.
Lino provides a solution for Django's old problem of not having an "application server startup signal", a signal to be emitted when the models cache has been populated.
About the problem¶
The problem is old:
In March 2010, wojteks suggested to call it "server_initialized" in his Django ticket #13024 ("Signal sent on application startup"). This ticket has been closed because it was "fixed in a branch which needs review. See #3591."
Django ticket #3591 ("add support for custom app_label and verbose_name") seems truly very interesting and truly very complex, but didn't get into 1.5. Obviously it's not easy to find a good solution.
Note that this is not the same problem as in Entry point hook for Django projects (2013-02-14) where Brian Rosner describes a method for "running code when Django starts". We don't want to run code when Django starts, but after Django has finished to start. The difference is important e.g. if you want to analyze all installed models.
How Lino solves it¶
The basic trick is to simply send the signal "at the end of your last app's models.py file" as described by Ross McFarland on Sun 24 June 2012.
Result is that you can now write code like the following in any models or admin module of your existing project:
from djangosite.signals import startup, receiver @receiver(startup) def my_handler(sender,**kw): # code to run exactly once per process at startup print sender.welcome_text()