3

Is there a way to hook on the loading of the django application? I want to be able to execute code when the application is loaded, so that I can for example create static variables to be used later on by the application or establish connections to other servers.

The best I came across was to add code in the __init__.py file (How do I create application scope variable django?) but the problem with this solution is that I want my code to be executed after django has finished its startup process, and not in the middle/start of it.

Another solution I came up with is to have a view that handles this process and then when the application is deployed I issue a request to the url of the view. I don't like this solution very much, I prefer it to be a part of the loading of the application.

Any ideas of how to pull this out? Thanks;

edit: Apllication refers to the entire django project and not one of the INSTALLED_APPS

Community
  • 1
  • 1
Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
  • Have you considered making it lazy (create connection when you need it and save it for later use, etc). – Marius Grigaitis Mar 08 '12 at 22:31
  • This solution is great if you don't mind having a few instances/connections made. What happens if request A comes in, I start up everything and while it's being processed request B comes, then I either bootstrap again, or need to sync requests which I'm not about to do. Also, it takes time to start up everything I need, and so the first request will take way to much time to load and I would like to avoid that. – Nitzan Tomer Mar 09 '12 at 09:59
  • I think you can fix many instances problem by using some kind of locks (but I don't know how it will work when you're running multiple processes on wsgi). – Marius Grigaitis Mar 09 '12 at 10:37
  • Using locks between requests sounds like a bad idea to me, it's a slippery road to take. – Nitzan Tomer Mar 09 '12 at 18:21

1 Answers1

1

Right now, there's really no good way to do this as Django doesn't have a startup signal. Interestingly, there is a ticket for this, but it's strangely tied to a branch that is being held up by another ticket. I'm not sure if Django 1.4 is feature-locked, yet, but as it's in release candidate stage, my bet is that it is. So, maybe you might get this in Django 1.5 whenever that happens.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • Thanks for the info. Is there any kind of hack for this? What I'm doing now is running the server as usual then when it's finished I execute a script that issues a bootstrap request. I would love to make it automated though. – Nitzan Tomer Mar 09 '12 at 09:56
  • Some people recommend adding your startup stuff to urls.py, but putting random stuff into a file meant for your site's urlpatterns doesn't sit right with me. There's also a kind of trick with middleware (see: http://stackoverflow.com/questions/6791911/execute-code-when-django-starts-once-only), but that's hacky and not ideal either. – Chris Pratt Mar 09 '12 at 13:25
  • I tried the middleware solution but it's not what I'm looking for since it only get trigger as part of the requests handling, and not on startup. As for using the *urls.py*, I'm with you on that one. – Nitzan Tomer Mar 09 '12 at 18:27
  • Anything new on this topic? Having similar problem, tried many things but nothing works. – krizajb Sep 14 '12 at 06:36
  • Honestly, I'm not sure what's going on with this. It's fixed, but it's tied to another feature (which is also fixed), but they're both on some unspecified branch. The only reference I can find to another branch is the app-loading refactor, but that was committed to master 2 years ago. I think this has just gotten lost in the shuffle, honestly. It might be worthwhile to open a new ticket (referencing this one), and explain that this still hasn't made it to master despite being complete for almost a year. – Chris Pratt Sep 17 '12 at 14:23