mod_wsgi 的两种模式

http://ssmax.net/archives/977.html

http://www.cnblogs.com/yuxc/p/3555005.html

mod_wsgi 有两种运行模式,

第一种是嵌入模式,类似于mod_python,直接在apache进程中运行,这样的好处是不需要另外增加进程,但是坏处也很明显,所有内存都和apache共享,如果和mod_python一样造成内存漏洞的话,就会危害整个apache。而且如果apache是用worker mpm,mod_wsgi也就强制进入了线程模式,这样子对于非线程安全的程序来说就没法用了。

这种模式下只需要在apache下面设置
WSGIScriptAlias /path /path-to-wsgi

即可生效,对于小型脚本的话,直接用这种模式即可。

第二种是后台模式,类似于FastCGI的后台,mod_wsgi会借apache的外壳,另外启动一个或多个进程,然后通过socket通信和apache的进程联系。

这种方式只要使用以下配置即可开启:
#启动WSGI后台,site1是后台名字
WSGIDaemonProcess site1 processes=2 threads=15 display-name=%{GROUP}

#分配当前上下文应该使用哪个WSGI后台,可以放在Location里面指定
WSGIProcessGroup site1

#根据当前上下文的ProcessGroup分配到对应的后台
WSGIScriptAlias /path /path-to-wsgi

后台模式由于是与apache进程分离了,内存独立,而且可以独立重启,不会影响apache的进程,如果你有多个项目(django),可以选择建立多个后台或者共同使用一个后台。

比如在同一个VirtualHost里面,不同的path对应不同的django项目,可以同时使用一个Daemon:

WSGIDaemonProcess default processes=1 threads=1 display-name=%{GROUP}

WSGIProcessGroup default

WSGIScriptAlias /project1 “/home/website/project1.wsgi”

WSGIScriptAlias /project2 “/home/website/project2.wsgi”

这样子两个django都使用同一个WSGI后台。

也可以把不同的项目分开,分开使用不同的后台,这样开销比较大,但就不会耦合在一起了。

display-name是后台进程的名字,这样方便重启对应的进程,而不需要全部杀掉。

WSGIDaemonProcess site1 processes=1 threads=1 display-name=%{GROUP}

WSGIDaemonProcess site2 processes=1 threads=1 display-name=%{GROUP}

<Location “/project1”>
WSGIProcessGroup site1
</Location>
WSGIScriptAlias /project1 “/home/website/project1.wsgi”

<Location “/project1”>
WSGIProcessGroup site2
</Location>
WSGIScriptAlias /project2 “/home/website/project2.wsgi”

对于django 1.0以下的版本,由于官方认定不是线程安全的,所以建议使用多进程单线程模式

processes=n threads=1

但是我自己在用django 0.9.6,使用多线程模式在很多项目里面基本都没有问题,包括在worker模式下面使用mod_python,其实是一样的道理,呵呵。

升级到django 1.0以后,就可以放心的使用多进程多线程模式了:

processes=2 threads=64

这样子性能会更好。

下面是两种模式的英文原文:

When hosting WSGI applications using mod_wsgi, one of two primary modes of operation can be used. In ’embedded’ mode, mod_wsgi works in a similar way to mod_python in that the Python application code will be executed within the context of the normal Apache child processes. WSGI applications when run in this mode will therefore share the same processes as other Apache hosted applications using Apache modules for PHP and Perl.

An alternate mode of operation available with Apache 2.X on UNIX is ‘daemon’ mode. This mode operates in similar ways to FASTCGI/SCGI solutions, whereby distinct processes can be dedicated to run a WSGI application. Unlike FASTCGI/SCGI solutions however, a separate infrastructure is not needed when implementing the WSGI application and everything is handled automatically by mod_wsgi.

Because the WSGI applications in daemon mode are being run in their own processes, the impact on the normal Apache child processes used to serve up static files and host applications using Apache modules for PHP, Perl or some other language is much reduced. Daemon processes may if required also be run as a distinct user ensuring that WSGI applications cannot interfere with each other or access information they shouldn’t be able to.

mod_wsgi 的两种模式的更多相关文章

  1. Windows2003 IIS6.0支持32位和64位两种模式的设置方法

    IIS 6.0 可支持 32 位和 64 位两种模式.但是,IIS 6.0 不支持在 64 位版本的 Windows 上同时运行这两种模式.ASP.NET 1.1 只在 32 位模式下运行.而 ASP ...

  2. 【转】Reactor与Proactor两种模式区别

    转自:http://www.cnblogs.com/cbscan/articles/2107494.html 两种IO多路复用方案:Reactor and Proactor 一般情况下,I/O 复用机 ...

  3. ACE_linux:Reactor与Proactor两种模式的区别

    一.概念: Reactor与Proactor两种模式的区别.这里我们只关注read操作,因为write操作也是差不多的.下面是Reactor的做法: 某个事件处理器宣称它对某个socket上的读事件很 ...

  4. JSP中两种模式的总结

    运用JSP/Servlet实现的Web动态交互,主要采用: 模式一:JSP+JavaBean 链接:http://wxmimperio.coding.io/?p=155 模式二;JSP+Servlet ...

  5. Doctype 严格模式与混杂模式-如何触发这两种模式,区分它们有何意义?

    Doctype:(Document Type)文档类型,它位于文档中最前面的位置,处于标签之前.如果你想制作符合标准的页面,一个必不可少的关键组成部分就是DOCTYPE的声明.确定了正确的Doctyp ...

  6. Android Studio的两种模式及签名配置

    我们使用Android Studio 运行我们的app,无非两种模式:debug和release模式. debug模式 debug模式使用一个默认的debug.keystore进行签名. 这个默认签名 ...

  7. vue-router的两种模式的区别

    众所周知,vue-router有两种模式,hash模式和history模式,这里来谈谈两者的区别. ### hash模式 hash模式背后的原理是`onhashchange`事件,可以在`window ...

  8. Doctype的作用?严格模式与混合模式,如何触发者这两种模式,区分它们有何意义?

    Doctype作用?严格模式与混合模式,如何触发者这两种模式,区分它们有何意义? 1.1 Doctype作用 <!DOCTYPE>声明叫做文件类型定义(DTD),声明的作用为了告诉浏览器该 ...

  9. 【Spark篇】---SparkStreaming+Kafka的两种模式receiver模式和Direct模式

    一.前述 SparkStreamin是流式问题的解决的代表,一般结合kafka使用,所以本文着重讲解sparkStreaming+kafka两种模式. 二.具体 1.Receiver模式    原理图 ...

随机推荐

  1. codeforces701C

    They Are Everywhere CodeForces - 701C 大B,年轻的口袋妖怪训练师,找到了一个由 n 间从左向右的房间组成的大房子.你可以从街道里走入任何一间房子,也可以从任何一间 ...

  2. BZOJ1069 SCOI2007最大土地面积(凸包+旋转卡壳)

    求出凸包,显然四个点在凸包上.考虑枚举某点,再移动另一点作为对角线,容易发现剩下两点的最优位置是单调的.过程类似旋转卡壳. #include<iostream> #include<c ...

  3. UUID类型如何比较是否相等

    直接使用equals方法 UUID x = UUID.fromString("062db347-6a72-69a1-40c0-7516e0a26459"); UUID y = UU ...

  4. MT【216】韦达定理

    设$n$为正整数,$a_1,a_2,\cdots,a_n;b_1,b_2,\cdots,b_n;A,B$都是正数, 满足$a_i\le b_i,a_i\le A,i=1,2,\cdots,n$ 且$\ ...

  5. MT【205】寻找对称中心

    函数$f(x)=\dfrac{x}{x+1}+\dfrac{x+1}{x+2}+\cdots+\dfrac{x+2018}{x+2019}$ 的图像的对称中心_____ 提示:根据定义域可知如果有对称 ...

  6. loj #117. 有源汇有上下界最小流

    题目链接 有源汇有上下界最小流,->上下界网络流 注意细节,边数组也要算上后加到SS,TT边. #include<cstdio> #include<algorithm> ...

  7. NodeJS - Express 4.0错误:Cannot read property 'Store' of undefined

    按着<NodeJS开发指南>里的第五章建立microblog的例子操作,使用node.js 的express框架配置将session存储到mongodb时出错:TypeError: Can ...

  8. Android在初始化时弹出popwindow的方法

     http://blog.csdn.net/sxsboat/article/details/7340759 Android中在onCreate()时弹出popwindow,很多人都有过类似的需求吧,但 ...

  9. Ubuntu16.04中禁用UTC解决双系统时间问题

    解决方法一 sudo hwclock -w --localtime 解决方法二 timedatectl set-local-rtc 1 解决方法三 修改/etc/adjtime文件中的UTC,为LOC ...

  10. A1012. The Best Rank

    To evaluate the performance of our first year CS majored students, we consider their grades of three ...