You're right, since it is asynchronous it will not be ready at the time 
settings are read.

The db lookup occurs only once for the life of the process, not on request.

The app must first look up in a directory (which path I put in settings...) 
if there are any sqlite files that are not already in the main database. If 
there are some new, it updates the main database with these. Then from the 
settings, it reads the updated main database to get all the connections 
names, that it puts into DATABASES. The the app can start; every user is 
given access to a couple of these sqlite and can switch from one to the 
other - this works fine at least.

Running that from inside the settings.py, or from apps.ready(), satisfies 
the order of events, but unfortunately it is executed as many times as 
there are processes, so the inserts are duplicated. I tried setting a 
global flag variable to mark that the code already ran, but it did not 
work. I tried "insert if not exists" kinds of sql stuff without success. 
Maybe a try/catch on a database lock will be more fortunate.


Le lundi 25 avril 2016 15:13:02 UTC+2, Julien Delafontaine a écrit :
>
> Hi again,
>
> It seems trememdously hard in Django to run code only once when the app 
> starts, instead it is executed once per process. I tried wsgi.py, 
> settings.py, apps.py, and many others. Is it a good idea to use the 
> --service-script option for that purpose?
>
> I want to scan a folder when the app starts to update my database 
> according to the files found there.
> Like
>     --service-script scan myapp/scripts/scan_folders.py
> even if I don't use the service anymore after that.
>

-- 
You received this message because you are subscribed to the Google Groups 
"modwsgi" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/modwsgi.
For more options, visit https://groups.google.com/d/optout.

Reply via email to