Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Processing some file before starting app and reacting for every change

Tags:

python

django

I have a file containing some data – data.txt (existing in proper localization). I would like that django app processes this file before starting app and reacts for every change (without restart). What is the best way to do it?

like image 311
David Silva Avatar asked Apr 20 '13 16:04

David Silva


2 Answers

For startup you can write middleware that does what you want in init and afterwards raise django.core.exceptions.MiddlewareNotUsed from the init, so the django will not use it for any request processing. docs

And middleware init will be called at startup, not at the first request. As for react to file changes you can use https://github.com/gorakhargosh/watchdog ( an example of usage can be found here). So you can either start it somewhere in middleware too, or if its only db updates you can create a separate script(or django management command) that will be run via supervisor or something like this and will monitor this file and update the db.

like image 109
Aldarund Avatar answered Oct 05 '22 09:10

Aldarund


An option could be pynotify that monitors the filesystem for changes, it works only on Linux though.

Otherwise look at the code of runserver command that seems to do the same thing (the code of the autoreload module is here).

To run a command before starting an app I suppose you can write some code in the settings module.

like image 23
gipi Avatar answered Oct 05 '22 09:10

gipi