When a Lino application starts up

This chapter describes what happens during the startup of a Lino process.

There are four major phases in the startup of a Lino process:

  • Application definition : while Django settings are being loaded.

  • Database definition : while Django models are being loaded

  • Analysis when Django models are loaded and Lino analyzes its own data structures.

  • Runtime (lino.api.rt) when startup has finished.

There are three major lino.api modules named after these phases:

  • lino.api.ad is available during Application Definition.

  • lino.api.dd is available when AD has finished, during DD and Analysis.

  • lino.api.rt is available when Analysys has finised.

A more detailed description follows.

  • The manage.py script causes the module specified by DJANGO_SETTINGS_MODULE (your settings.py module) to be imported. This might happen twice (e.g. with runserver). Everything in lino.api.ad is usable.

  • Importing the settings.py module will instantiate your SITE.

  • When settings are ready, Django will load the models.py modules. Everything in lino.api.dd is usable during this step.

  • When all models are loaded, the Site will "start up" and instantiate the kernel. Only now everything in lino.api.rt is usable.

Analysis

The remaining part of this section is obsolete. It was for Django before 1.7.

A server startup signal for Django

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.

That's why lino must be the last item of your INSTALLED_APPS.

Although lino doesn't have any model of its own, it does have a models module which invokes the startup method. The startup method then emits a startup signal.

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()