0

I currently have a sizable django project. In the view, I do a large computation that is shared between views to save time on later requests. To activate the site, I have a python script that kicks off a number of scripts through manage.py for things like creating symlinks, collecting static files, etc. When each of the commands runs, it loads all the apps in the project, which does this large computation. I want to prevent this. Ideally, I'd like the precomputation to happen only once when I activate. I think I need to either:

  1. Prevent manage.py from actually loading the view (not sure this is possible)
  2. Lazily initialize the computation (not ideal since the first person to request a resouce will take multiple minutes)

What are my options? Any help is appreciated!

Championcake
  • 193
  • 3
  • 10

1 Answers1

0

You can use AppConfig for this, more specifically AppConfig.ready()

See this SO answer or documentation on how to do this.

Edit:

One option is to run a script manually from django shell. You can do this by typing python manage.py shell in terminal. You can then import models, views and similar to shell and execute you script or function.

Another option is to bind the computation to a view that is not used for anything else, and adding an url-path to run this function. Then you can manually go to this url and the calculation will be run.

Would these methods be suitable?

Community
  • 1
  • 1
Martin Hallén
  • 1,492
  • 1
  • 12
  • 27
  • This is what I tried first, but each time I run manage.py it will create a new AppConfig, and the AppConfig.ready() will run. – Championcake Jun 17 '16 at 21:27