使用 django 和 mod_wsgi 为每个请求重新加载 Python 模块
我在模块的 init 中有一个变量,它从数据库加载,大约需要 15 秒。
对于 django 开发服务器,一切工作正常,但看起来像 apache2 和 mod_wsgi 模块会随每个请求加载(需要 15 秒)。
对这种行为有什么想法吗?
更新:我已经在mod wsgi中启用了守护进程模式,看起来它现在没有重新加载模块!需要更多测试,我会更新。
I have a variable in init of a module which get loaded from the database and takes about 15 seconds.
For django development server everything is working fine but looks like with apache2 and mod_wsgi the module is loaded with every request (taking 15 seconds).
Any idea about this behavior?
Update: I have enabled daemon mode in mod wsgi, looks like its not reloading the modules now! needs more testing and I will update.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能忽略了这样一个事实:在 mod_wsgi 或 mod_python 的嵌入式模式下,应用程序是多进程的。因此,请求可能会发送到不同的进程,并且在第一次遇到之前未命中的进程时,您会看到延迟。在 mod_wsgi 守护进程模式下,默认只有一个进程。或者正如其他人提到的,您将 MaxRequestsPerChild 设置为 1,这是一个非常糟糕的主意。
You were likely ignoring the fact that in embedded mode of mod_wsgi or with mod_python, the application is multiprocess. Thus requests may go to different processes and you will see a delay the first time a process which hasn't been hit before is encountered. In mod_wsgi daemon mode the default has only a single process. That or as someone else mentioned you had MaxRequestsPerChild set to 1, which is a really bad idea.
我猜想,您的 Apache 设置中的 MaxClients / MaxRequestsPerChild 和/或 ThreadsPerChild 的值为 1。因此 Apache 必须为每个 mod_python 调用启动 Django。这就是为什么花了这么长时间。如果您有 wsgi 守护程序,则仅当您“触摸”wsgi 脚本时才会重新启动。
I guess, you had a value of 1 for MaxClients / MaxRequestsPerChild and/or ThreadsPerChild in your Apache settings. So Apache had to startup Django for every mod_python call. That's why it took so long. If you have a wsgi-daemon, then a restart takes only place if you "touch" the wsgi script.