tags:

views:

130

answers:

4

I want to perform some one-time operations such as to start a background thread and populate a cache every 30 minutes as initialize action when the Django server is started, so it will not block user from visiting the website. Where should I place all this code in Django?

  • Put them into the setting.py file does not work. It seems it will cause a circular dependency.
  • Put them into the __init__.py file does not work. Django server call it many times (What is the reason?)
+1  A: 

For one operation in startserver, you can use customs commands or if you want a periodic task or a queue of taske you can use celery

diegueus9
A: 

__init__.py will be called every time the app is imported. So if you're using mod_wsgi with Apache for instance with the prefork method, then every new process created is effectively 'starting' the project thus importing __init__.py. It sounds like your best method would be to create a new management command, and then cron that up to run every so often if that's an option. Either that, or run that management command before starting the server. You could write up a quick script that runs that management command and then starts the server for instance.

f4nt
+6  A: 

I just create standalone scripts and schedule them with cron. Admittedly it's a bit low-tech, but It Just Works. Just place this at the top of a script in your projects top-level directory and call as needed.

#!/usr/bin/env python
from django.core.management import setup_environ
import settings
setup_environ(settings)
from django.db import transaction

# random interesting things
# If you change the database, make sure you use this next line
transaction.commit_unless_managed()
Peter Rowell
i think if you use a cron job is even better use celery.
diegueus9
@diegueus9: OK, *why* is celery better? Specifically? I've googled on it and it seems ... not quite ready for primetime. When I'm doing stuff for production I get *really conservative*. I've seen waaay too many wonderful Next Great Things that somehow failed to fulfill my expectations. Cron may be crude/cryptic to a new user, but I've used it for 20 years and it is solid as a rock. That's what I want in production.
Peter Rowell
@peter IMHO, i think, because:1) Is a aplication for django2) Celery have a queue of tasks, cron not.3) Maybe because there is not cron in all OS.4) there is a lot of features i think will be hard with cron http://celeryproject.org/introduction.html#features5) I think the architecture client/server of celery and the workers is more powerfull than cron jobs.By the way, sorry for my bad english.
diegueus9
+2  A: 

We put one-time startup scripts in the top-level urls.py. This is often where your admin bindings go -- they're one-time startup, also.

Some folks like to put these things in settings.py but that seems to conflate settings (which don't do much) with the rest of the site's code (which does stuff).

S.Lott
I tried putting the init method into the urls.py, but seems it won't be called before any visiting