python - Why does manage.py execution script run twice when using it under if __name__ == "__main__" -


goal. when launching django framework launch other py scripts rely on django objects. server , port number config file.

problem: popen seems run twice , i'm not sure why?

#!/usr/bin/env python import os import sys import subprocess os.environ.setdefault("django_settings_module", "test.localsettings") django.core.management import execute_from_command_line  def getargs():      try:         f = open("config")         data = []         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 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 e:         print e         pass      print "end"      execute_from_command_line(getargs()) 

outputs:

16200 end 29716 end validating models... 

this first attempt if knows of better way feel free let me know.

thanks everyone.

your code launching runserver command, causes django use reloader, in turn means code reexecuted if entered on command line. if use --noreload when launch runserver issue disappear.

so basically, same facility automatically reloads django when modify source files, useful in development, causing code execute twice.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -