【问题标题】:How to warm up django web service before opening to public?如何在向公众开放之前预热 django Web 服务?
【发布时间】:2019-09-27 06:42:30
【问题描述】:

我正在 AWS ecs 上运行 django Web 应用程序。

我想在部署新版本时预热服务器(点击第一个请求,django 需要一些时间才能加载)。

有没有办法在将服务器注册到应用程序负载均衡器之前预热服务器?

  • 编辑

我正在使用 nginx + uwsgi

【问题讨论】:

    标签: django amazon-web-services docker amazon-ecs


    【解决方案1】:

    我假设您使用 mod_wsgi ,因为这是 here 描述的行为:

    Q: Why do requests against my application seem to take forever, but then after a bit they all run much quicker?
    
    A: This is because mod_wsgi by default performs lazy loading of any application. That is, an application is only loaded the first time
    

    一个针对该 WSGI 应用程序的请求到达。这表示 这些初始请求将产生加载所有 应用程序代码并执行任何启动初始化。

    This startup overhead can appear to be quite significant, especially if using Apache prefork MPM and embedded mode. This is
    

    因为每个进程和 prefork 都会产生启动成本 MPM 如果使用worker MPM,通常会有更多的进程 或 mod_wsgi 守护进程模式。因此,有多少进程就有多少请求 将缓慢运行,并且只有在代码运行后才能全速运行 全部加载完毕。

    Note that if recycling of Apache child processes or mod_wsgi daemon processes after a set number of requests is enabled, or for
    

    嵌入式模式 Apache 决定自己获取任何子节点 进程,然后您可以定期看到这些延迟的请求 发生。

    Some number of the benchmarks for mod_wsgi which have been posted do not take into mind these start up costs and wrongly try to compare
    

    结果到其他系统,例如 fastcgi 或基于代理的系统 默认情况下将预加载应用程序代码。因此 mod_wsgi 被描绘得比现实更糟糕。如果 mod_wsgi 是 正确配置结果会比显示的要好 那些基准。

    For some cases, such as when WSGIScriptAlias is being used, it is actually possible to preload the application code when the processes
    

    首先开始,而不是在第一个请求到达时。预加载 应用程序请参阅 WSGIImportScript 指令。

    我想你可以尝试使用WSGIScriptAlias 查看更多here

    【讨论】:

    • 我用的是 nginx + uwsgi,我猜 mod_wsgi 是 apache?
    • 在 nginx 中有一个 --lazy-apps 选项替换 fork ,也许这里有帮助
    【解决方案2】:

    我刚刚将健康检查从基于 nginx 的健康检查更改为与 uwsgi 相关的健康检查,

    在 django 中创建一个端点,并让 ELB 将其用作健康检查

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-18
      相关资源
      最近更新 更多