Django Startup code

195 views
Skip to first unread message

Maor

unread,
Oct 26, 2010, 12:25:06 PM10/26/10
to PyWeb-IL
Hi all,

I'm having trouble finding where to put code that should run on django
server start-up. I need it for a daemon thread that polls another
system. It cannot wait for the first user to hit the site.

I tried two things that didn't work:

1. Create a fake middleware. On some forums I read that this is the
right way to go, but it doesn't work. My middleware init code runs
when the first user hits the site.

2. Run my code from settings.py. This is ugly but should work. But
it doesn't work well in debug mode. In debug there is a second
process that detects changes in the file system to support automatic
reload. This process also loads settings.py and therefore starts my
daemon. :(

Any ideas anyone?

Thanks a lot,
-Maor

oz katz

unread,
Oct 26, 2010, 12:37:02 PM10/26/10
to pywe...@googlegroups.com
Hi,

Your question is a bit confusing.
I wouldn't run something like that as a thread on my web server.
I would implement it as an all together separate process that does the polling and updates the DB.
this could of course be a Django stand alone script. but should not be dependent on the web server itself.

on some implementations I know that the web server spawns several Python interpreters,
each of which will run your thread, which might result in undesired effects.

Oz


--
You received this message because you are subscribed to the Google Groups "PyWeb-IL" group.
To post to this group, send email to pywe...@googlegroups.com.
To unsubscribe from this group, send email to pyweb-il+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pyweb-il?hl=en.


Shai Berger

unread,
Oct 26, 2010, 4:54:09 PM10/26/10
to pywe...@googlegroups.com
Hi,

Just one comment to Oz's correct (IMHO) note:

On Tuesday 26 October 2010, oz katz wrote:
>
> I wouldn't run something like that as a thread on my web server.
> I would implement it as an all together separate process that does the
> polling and updates the DB.
> this could of course be a Django stand alone script.

The natural place for it would be a management command.

Idan Gazit

unread,
Oct 26, 2010, 5:39:04 PM10/26/10
to pywe...@googlegroups.com
Or you could use a task queue like celery -- it's ideally suited to this sort of thing.

I don't quite understand the "run once at startup" thing; I can't think of a situation where I'd want to run something django-related just once for a given server.

However, if you do have such a use-case, then yes -- a django management command is the way to go. It will make your life simpler since management commands already run in a viable django context. You simply need to trigger a script upon server start that executes "manage.py yourcommand". If your task is repeating, then run the management command from within a cron.

-I

Reply all
Reply to author
Forward
0 new messages