在 django+mod_wsgi+apache 上初始化模块
我的 django 应用程序在 apache+wsgi 上运行。我的 django 应用程序中的模块之一需要通过 jpype 加载 Java 库,并且由于其应用程序性质,该 Java 库需要很长时间才能初始化。
问题是,对于 apache+wsgi 设置中 django 处理的每个 http 请求,都会重新加载该 Java 库。但是,当我在开发 Web 服务器(python manager.py runserver 8000)中运行相同的应用程序时,不会发生这种情况。在开发 Web 服务器中,它仅加载 Java 库一次。
有没有办法更改 apache 或 mod_wsgi 配置或我的 django 应用程序,以便它不会为每个 http 请求重新加载我的 Java 库?
非常感谢。
安迪
My django application is running on apache+wsgi. One of the module in my django app needs to load a Java library via jpype and this Java library takes long time to initialize due to its application nature.
The problem is that, for each http request handled by django in apache+wsgi setup, this Java library is re-loaded. However, this does not happen when I run my same app in development web server (python manager.py runserver 8000). In development web server, it only loads the Java library only once.
Is there any way to change apache or mod_wsgi configuration or my django app so that it won't reload my Java library for every http request?
Many thanks.
Andy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能只是感到困惑,并且实际上正在使用糟糕的 Apache/mod_wsgi 配置。具体来说,您可能正在将嵌入式模式与 Apache prefork MPM 一起使用。这很糟糕,因为 Apache 将使用大量单线程进程,因此必须将代码加载到所有进程中。这就是为什么您可能认为它发生在针对同一进程的每个请求上,而实际上,每个请求都在访问不同的进程。
确保您使用的是 mod_wsgi 的守护进程模式,并且您的代码是线程安全的,因此使用单个多线程进程,应该不会出现问题。
编辑您的问题并从 Apache 配置文件添加 Apache/mod_wsgi 配置片段,并说明您正在使用的 Apache MPM。
You are possibly just getting confused and are actually using as poor Apache/mod_wsgi configuration. Specifically, you are likely using embedded mode with Apache prefork MPM. That is bad because Apache will use lots of single thread processes and so the code has to be loaded in all of them. That is why you probably think it is happening on each request against the same process, where in reality, each request is hitting a different process.
Ensure you are using daemon mode of mod_wsgi and that your code is thread safe and so use single multithreaded process and it shouldn't have the issue.
Edit your question and add your Apache/mod_wsgi configuration snippets from Apache configuration file and state what Apache MPM you are using.