0

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.

siamii
  • 23,374
  • 28
  • 93
  • 143

1 Answers1

0

If you need to start a subprocess on startup, there are a few bits to clear up:

  1. By 'startup', you have to mean when an individual Django thread is started.
  2. 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