I'd like to execute some python code after Django server startup. This code has an infinite loop and will run in the background until server shutdown. It accesses the database and various internet services. At the same time users should be able to use the front end webpages and have database access as well.
Asked
Active
Viewed 1,854 times
0
-
1@Amber celery is a sophisticated asynchronous queue management system. All I need is start one background thread on startup – siamii Mar 10 '13 at 22:06
-
celery isn't the only answer listed on that question. – Amber Mar 10 '13 at 22:14
-
You need to start a thread or a subprocess. There are many options to do that, and plenty of questions here that ask that question, with answers. – Lennart Regebro Mar 10 '13 at 22:17
-
@LennartRegebro The linked question doesn't explain how to execute the code at startup – siamii Mar 10 '13 at 22:24
-
@bizso09: If you find the answers unclear you can comment on them there for further explanation. – Lennart Regebro Mar 11 '13 at 07:42
1 Answers
0
If you need to start a subprocess on startup, there are a few bits to clear up:
- By 'startup', you have to mean when an individual Django thread is started.
- If you've got Django under a multiple-thread-proxying setup, and you just want to have one subprocess, regardless of how many Django threads are running on your box, you'll need to sort out some way of either checking whether your subprocess has already been started, or turn your 'subprocess' into a full-formed process/service of its own, and use some type of daemon manager to start both Django and your subprocess.
That said, the easiest place to patch in would likely be settings.py
. This file/module is imported once, when a Django thread starts up.
You could also do it within wsgi.py
, if you're using a WSGI proxying setup.
For the 'how to start a thread' answer, please see this one.

Community
- 1
- 1

Jack Shedd
- 3,501
- 20
- 27