- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我对 mod_wsgi 的多处理功能以及将在具有多处理能力的 WSGI 服务器上执行的 WSGI 应用程序的一般设计有点困惑。
考虑以下指令:
WSGIDaemonProcess example processes=5 threads=1
如果我理解正确,mod_wsgi 将生成 5 个 Python(例如 CPython)进程,并且这些进程中的任何一个都可以接收来自用户的请求。
文档说:
Where shared data needs to be visible to all application instances, regardless of which child process they execute in, and changes made to the data by one application are immediately available to another, including any executing in another child process, an external data store such as a database or shared memory must be used. Global variables in normal Python modules cannot be used for this purpose.
但是在这种情况下,当人们想要确保应用程序在任何 WSGI 条件下(包括多处理条件)运行时,它会变得非常繁重。
例如,一个包含当前连接用户数量的简单变量 - 它应该是进程安全的从 memcached 读/写到 memcached 还是一个 DB 或(如果这种超出标准的库机制是可用)共享内存?
代码会喜欢
counter = 0
@app.route('/login')
def login():
...
counter += 1
...
@app.route('/logout')
def logout():
...
counter -= 1
...
@app.route('/show_users_count')
def show_users_count():
return counter
在多处理环境中表现不可预测?
谢谢!
最佳答案
您的问题需要考虑几个方面。
首先,apache MPM 和 mod_wsgi 应用程序之间的交互。如果您在嵌入式模式下运行 mod_wsgi 应用程序(不需要 WSGIDaemonProcess
,WSGIProcessGroup %{GLOBAL}
),您将从 apache MPM 继承多处理/多线程。这应该是最快的选项,并且您最终会拥有多个进程和每个进程的多个线程,具体取决于您的 MPM 配置。相反,如果您在守护程序模式下运行 mod_wsgi,WSGIDaemonProcess <name> [options]
和 WSGIProcessGroup <name>
,您可以以较小的 overhead 为代价对多处理/多线程进行精细控制.
在单个 apache2 服务器中,您可以定义零个、一个或多个命名 WSGIDaemonProcess
es,并且每个应用程序都可以在这些进程之一中运行( WSGIProcessGroup <name>
)或在嵌入式模式下运行 WSGIProcessGroup %{GLOBAL}
.
您可以通过检查 wsgi.multithread
来检查多处理/多线程。和 wsgi.multiprocess
变量。
根据您的配置WSGIDaemonProcess example processes=5 threads=1
您有 5 个独立的进程,每个进程都有一个执行线程:没有全局数据,没有共享内存,因为您无法控制生成子进程,但 mod_wsgi 正在为您完成。为了共享全局状态,您已经列出了一些可能的选项:进程接口(interface)的数据库、某种基于文件系统的持久性、守护进程(在 apache 之外启动)和基于套接字的 IPC。
正如 Roland Smith 所指出的,后者可以通过 multiprocessing.managers
使用高级 API 来实现。 : 在 apache 之外创建并启动 BaseManager
服务器进程
m = multiprocessing.managers.BaseManager(address=('', 12345), authkey='secret')
m.get_server().serve_forever()
在你的应用程序中,你 connect
:
m = multiprocessing.managers.BaseManager(address=('', 12345), authkey='secret')
m.connect()
上面的例子是假的,因为 m
没有注册有用的方法,但是 here (python 文档)您将了解如何在您的进程中创建和代理一个对象(如示例中的 counter
)。
对您的示例的最后评论,带有 processes=5 threads=1
.我知道这只是一个示例,但在实际应用中,我怀疑性能与 processes=1 threads=5
相当。 :只有在“单进程多线程”模型的预期性能提升显着的情况下,您才应该了解在多处理中共享数据的复杂性。
关于Python、WSGI、多处理和共享数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12715139/
只是想知道是否有可能找出谁从 Windows 共享中读取了文件(最好使用 .NET,但 win32 native 可以)? 我想做的是创建类似 awstats 的东西对于 Windows 共享,这样我
是否可以列出 Intent.ACTION_SEND ?我的意思是我需要知道是否有人通过 action_send 在 Facebook 上分享或在 Twitter 上发推文。 最佳答案 也许你想要一个更
我正在使用 Google Apps 应用程序。实际上,我想在不使用密码的情况下访问另一个 ID。我使用了 OAuth,它运行良好。但我无法分享特定人的日历。我尝试了以下代码。 GoogleOAuthP
我怎样才能只创建模拟器...可能吗?我知道,设备需要分发证书。 最佳答案 您只需将应用程序目录从 iPhone 模拟器复制到另一个实例/操作系统版本,它就应该可以工作。 因此,如果您想分发 3.1.3
我想使用多阶段构建来避免每次构建应用程序时都下载我的 Java 项目所需的所有 Maven 依赖项。 我正在考虑在第一阶段解决 Maven 依赖项,然后在第二阶段构建应用程序,这将需要访问在前一阶段下
我正在寻找保护用户下载内容的初步想法。用户下载充满有趣资源的 zip 文件,这些资源被提取到本地文件系统中以供应用程序使用。我的目标是防止用户通过互联网将下载的资源共享给其他用户(假设他们获得了对文件
我想知道在具有移动和桌面版本的网站上共享身份验证、 session 管理等的最佳方法是什么。我们正在运行 Tomcat,并且更愿意将移动站点和桌面站点的应用程序保持在不同的节点上。 我看过类似的帖子,
我发现了这个单例的实现。我怎样才能创建指向它的指针或共享指针?` 为什么这不起作用?自动测试 = Singleton::Instance(); class Singleton { public: st
我有一个 heroku 项目,我想与其他人分享。作为the instructions describe ,我使用 virtualenv 来管理环境和依赖项。有没有办法在新机器上从 requiremen
Maven 将所有 jar 存储在本地存储库 ~/.m2/repository/ 下。用户多时占用空间大。 那么,是否可以由多个用户共享这个本地存储库,或许在不同的目录结构下? 最佳答案 简单的回答
为什么共享 worker 在重新加载页面时死了?应该是复活了我该如何解决这个问题? 重装前 重新加载后(在example.com上按F5) parent worker var port = new S
我正在开发多个小型应用程序,这些应用程序将共享通用和共享模块和 Assets 。 关于如何创建项目结构的部分在这里回答:https://stackoverflow.com/a/61254557/135
我在 RHEL 上安装了 jenkins (localhost:8080),我能够成功地构建代码 现在,我想设置主/从代理。 我的笔记本电脑将充当“Master Jenkins”,而我同事的笔记本电脑
我有这种方法可以根据我使用的 EXTRA_STREAM 共享文本文件或图片。我有这两个我可以选择 i.putExtra(Intent.EXTRA_STREAM, uri); i.putExtra(In
我正在使用 R 中的一个数据分析项目,我正在使用 R 中的敏感私有(private)数据进行一些逻辑和多级建模。我爱上了 。预订 包,我已经创建了一本关于我们的工作流程和分析管道的相当广泛的书。问题是
我正在构建的应用程序需要在 UITabBarController 框架内为多个 View (及其 subview )显示共享的自定义 UIToolbar。自定义工具栏的内容在所有 View 中都是相同
我有多个应用程序,我想共享相同的 eslint 配置: - project_root/ - app1/ - node_modules/ - eslint.rc
我有多个 Electron 应用程序。一个是主应用程序,其他几个功能应用程序。主应用程序上的按钮很少,这将导致功能应用程序打开。这里的问题是每个应用程序都有一个主进程,该进程导致要利用更多的CPU。是
我正在开发一个 Node.js 后端,它通过 websocket 与一些桌面客户端进行通信,而服务器端的通信是从 Web 前端发起的。一切正常,因为我将 SockJS Connection 实例存储在
我对托管多个网站的服务器上的多个用户帐户使用私有(private) SSH key 和无密码条目。 我为每个用户帐户使用相同的私钥。 (因为我很懒?或者那是“正确”的方式)。 我现在想授权该国不同地区
我是一名优秀的程序员,十分优秀!