3

Goal. When launching django framework also launch other PY scripts that rely on django objects. Get the server and port number from a config file.

Problem: The Popen seems to run twice and I'm not sure why?

#!/usr/bin/env python
import os
import sys
import subprocess
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test.localsettings")
from django.core.management import execute_from_command_line

def getargs(): 
    try:
        f = open("config")
        data = []
        for line in f:
            data.append(line)
        f.close()
        server = data[0].rstrip()
        port = data[1]
        newargs = ['lmanage.py', 'runserver', server + ':' + port]
        return newargs

    except Exception as e:
        print e
        pass

if __name__ == "__main__":

    #Launching Checker
    try: 
        checker = subprocess.Popen([sys.executable, os.path.join(os.getcwd() + "checker.py")], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
        print checker.pid
    except Exception as e:
        print e
        pass 
    print "end"

    execute_from_command_line(getargs())

Outputs:

16200
end
29716
end
Validating models...

This is my first attempt so if anyone knows of a better way to do this then feel free to let me know.

Thanks everyone.

solarissmoke
  • 30,039
  • 14
  • 71
  • 73
cph
  • 458
  • 2
  • 6
  • 24
  • Is this script called `checker.py` by any chance? – Martijn Pieters Mar 11 '14 at 11:15
  • @Martijin Pieters No. The name of the script above is lmanage.py. – cph Mar 11 '14 at 11:17
  • @cph I'm pretty sure your problem is the reloader. Try issuing the `runserver` command with `--noreload`. – Louis Mar 11 '14 at 11:29
  • @Louis - You are correct. Did you want to answer for points? – cph Mar 11 '14 at 11:39
  • Just to link you to more information about other solutions: http://stackoverflow.com/questions/6791911/execute-code-when-django-starts-once-only – CasualDemon Mar 11 '14 at 11:40
  • @CasualDemon the watcher.py script has a While True that hopefully will run for a long time. Would this be blocking if launched from the top level urls.py file? – cph Mar 11 '14 at 11:52
  • @cph subprocess is asynchronous by default, so it should never block. I would actually look at the last answer provided in that link, apparently there is now a better way do to it also [outlined here](http://eldarion.com/blog/2013/02/14/entry-point-hook-django-projects/). Also as you are calling another python file, it would probably be better to simply import it and run it through multiprocessing. Hope that helps! – CasualDemon Mar 11 '14 at 12:01

1 Answers1

11

Your code is launching the runserver command, which causes Django to use the reloader, which in turn means that your code is reexecuted as if it were entered on the command line. If you use --noreload when you launch runserver the issue will disappear.

So basically, the same facility that automatically reloads Django when you modify your source files, which is so useful in development, is now causing your code to execute twice.

Louis
  • 146,715
  • 28
  • 274
  • 320