简体   繁体   English

为什么在__name__ ==“ __main__”下使用manage.py执行脚本时会运行两次

[英]Why does manage.py execution script run twice when using it under if __name__ == “__main__”

Goal. 目标。 When launching django framework also launch other PY scripts that rely on django objects. 启动django框架时,还要启动其他依赖django对象的PY脚本。 Get the server and port number from a config file. 从配置文件获取服务器和端口号。

Problem: The Popen seems to run twice and I'm not sure why? 问题:Popen似乎运行了两次,我不确定为什么吗?

#!/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. 感谢大家。

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. 您的代码正在启动runserver命令,这将导致Django使用reloader ,这反过来意味着您的代码将像在命令行中输入的那样被重新执行。 If you use --noreload when you launch runserver the issue will disappear. 如果在启动runserver时使用--noreloadrunserver此问题将消失。

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. 因此,基本上,使用相同的工具在修改源文件时会自动重新加载Django(这在开发中非常有用),现在会导致您的代码执行两次。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python manage.py shell &lt; scripts/myscript.py 在文件包含 if __name__ == &#39;__main__&#39; 时不起作用 - python manage.py shell < scripts/myscript.py not working when the file contains if __name__ == '__main__' 如果__name__ ==“ __main__”,该怎么写:如果整个.py文件是一个模块,并且永远不会以其自己的脚本运行? - What to write under if __name__ == “__main__”: if the entire .py file is a module and will never run as its own script? 使用 if __name__ == '__main__' 时代码不运行 - code doesnt run when using if __name__ == '__main__' 比在 Python 脚本中两次 if __name__ == &#39;__main__&#39; 更好的解决方案 - Better solution than if __name__ == '__main__' twice in Python script 如果__name__ ==“ __main__”,则Apache上托管的web.py不会在一边运行代码: - web.py hosted on Apache does not run code in side if __name__ == “__main__”: 如果 __name__==__main__ 运行,为什么我的 python 脚本会继续运行? - Why does my python script continue while if __name__==__main__ runs? 为什么IDLE不需要'if __name__ ==“__ main__”:运行测试用例,但是PyCharm呢? - Why doesn't IDLE need 'if __name__ == “__main__”: to run a test case, but PyCharm does? 使用 sphinx 记录 python 脚本条目 (__name__ == &#39;__main__&#39;) - Documenting python script entry (__name__ == '__main__') using sphinx 如何使用if __name__ ==&#39;__main__&#39;条件创建控制台脚本? - How to create a console script using if __name__ == ' __main__' condition? Mocking 没有 `if __name__=="__main__":` 块的脚本 - Mocking a script with no `if __name__=="__main__":` block
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM