2

I would like to create a thread (maybe in the future a process) that inherits all the django env and settings but goes off to do its own monitoring. Ideally I would like this to be triggered when the Django server is done initializing. I've tried both methods below. The first one seems to run the Middleware initializer every time a request is made and both of them have the problem that my startup code only gets triggered when the first request to the server is made.

Where to put Django startup code?

Running startup code right after Django settings? (also for commands)

I've also seen that someone checked in some signal for this exact use case but it will only be available in django 1.6 or later.

Any ideas how to work around this?

Thanks!

Community
  • 1
  • 1
rui
  • 11,015
  • 7
  • 46
  • 64

1 Answers1

2

I've never done this, but it looks like you can register your own command in Django admin, so you could make your own class to -- perhaps -- inherit from BaseRunserverCommand from the runserver.py module and overload the run method to trigger your thread or process.

(The current settings seem available in BaseRunserverCommand -- see the inner_run method, and I assume you can simply use os.environ to access the current environment.)

rorycl
  • 1,344
  • 11
  • 19