为什么django两次运行所有内容? [英] Why does django run everything twice?

查看:287
本文介绍了为什么django两次运行所有内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我在 settings.py 之类的任何位置(随机)的 print 语句或任何随机的<$ django中的c $ c> .py 文件,它被打印两次。为什么?

Whenever I put in a (random) print statement in somewhere like settings.py or any random .py file in django, it gets printed twice. Why?

尝试一下:

TEMPLATE_DIRS (在 settings.py 中),只需在其后添加打印语句:
print TEMPLATE_DIRS

after setting the value on TEMPLATE_DIRS (in settings.py), just add a print statement after it: print TEMPLATE_DIRS

,您的模板目录将被打印两次。或者,如果您感觉过时,只需在声明 TEMPLATE_DIRS 之后添加 print Hello World

and you will get your templates directory printed out twice. Or if you're feeling old-fashioned, just add print "Hello World right after declaring TEMPLATE_DIRS, and it will print it twice.

运行命令: python manage.py runserver

推荐答案

它没有运行两次,它分叉到2个进程,每个进程都运行一次。

It's not running it twice, it's forking to 2 processes and each one of those is running it once.

由以下人员回答:为什么初始化 django项目中的模块加载了两次

Answered by: why is init module in django project loaded twice


每个进程应该只加载一次...我猜是
manage.py分叉,并且启动了两个单独的进程。

It should be loaded only once... per process. I'm guessing that manage.py forks, and that two separate processes are launched.

一篇文章。

如果将其添加到 settings.py

import os
print(os.getpid())

它将打印出2个不同的进程ID号,表明它产生了2个进程。

It will print out 2 different process id numbers showing that it's spawned 2 processes.

您可以阅读有关分叉在Wikipedia上。

这似乎也是以下内容的重复:

This also seems a duplicate of:

django- settings.py似乎加载了多次?

这篇关于为什么django两次运行所有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆