【问题标题】:manage.py runserver is frequently reloading in productionmanage.py runserver 在生产中经常重新加载
【发布时间】:2020-04-11 13:32:44
【问题描述】:

我编写了一个 Django 2.2 程序,该程序在我的运行 Windows 10 的 PC 和运行 CentOS 7 的 VPS 上运行良好。

当我更改三个文件中的一些代码时,应用程序继续在本地工作,但在生产中表现异常。

当我跑步时

python manage.py runserver 0.0.0.0:80

在我的服务器上,我收到以下响应:

***\a.py changed, reloading.
Watching for file changes with StatReloader


***\b.py changed, reloading.
Watching for file changes with StatReloader


***\c.py changed, reloading.
Watching for file changes with StatReloader


***\a.py changed, reloading.
Watching for file changes with StatReloader


***\b.py changed, reloading.
Watching for file changes with StatReloader

我在部署之前修改了这些文件,但在站点运行时它们不应更改。

添加--noreload 标志可以防止这种情况:

python manage.py runserver --noreload 0.0.0.0:80

为什么需要这个?

【问题讨论】:

  • 为什么这些文件首先会发生变化?
  • 我正在编辑,需要新代码

标签: python django reloading


【解决方案1】:

manage.py runserver is only meant for development:

请勿在生产环境中使用此服务器。它没有经过安全审计或性能测试。 (这就是它会一直存在的方式。我们的业务是制作 Web 框架,而不是 Web 服务器,因此改进此服务器以使其能够处理生产环境超出了 Django 的范围。)

将 Gunicorn、Waitress 或 uWSGI 等 WSGI 服务器添加到您的依赖项并运行 that, e.g.:

gunicorn myproject.wsgi

通常,要在生产环境中部署 Gunicorn,您不会直接绑定到端口 80,而是绑定到 use something like Nginx

【讨论】:

  • 谢谢!在uwsgi中不行,所以我在命令行中测试一下
  • @betterpan,我不明白。这行得通吗?
  • 谢谢!在windows10下可以,在centos7 shell下不行。
  • 它永远重新加载,没有“--noreload”
  • ...但是您根本不应该在服务器上使用manage.py runserver。您是否尝试过我在回答中的建议?
【解决方案2】:

当您在生产环境中运行Django程序时,请在settings.py中将DEBUG = True的选项更改为DEBUG = False。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-12
    • 1970-01-01
    • 2015-10-10
    • 2018-01-23
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多