Django 多线程和 mod_wsgi
首先,我的网站已启动并运行正常。不存在严重问题。
但我想了解一些事情。
我将从概述我的系统开始。
这是一个由 django 驱动的站点,位于具有 256MB RAM 的 CentOS 5.3 VPS 上,在带有 mod_wsgi 的 apache 下。
django 应用程序作为具有 1 个线程的守护进程运行。
我的应用程序中需要的是: 1.初始化日志记录(目前正在工作,但我每次都会得到双重日志记录) 2.启动守护线程来做一些后台工作
现在,我已经阅读并实现了中提供的解决方案 http://blog.dscpl。 com.au/2010/03/improved-wsgi-script-for-use-with.html 但这并没有多大帮助。
基本上,我必须手动禁用工作 MPM(仅预分叉 MPM 正在运行)并将守护进程配置为一个进程和一个线程(由于某种原因,它不能与任何其他组合一起使用)。
但奇怪的是,该网站正在运行,并且正在创建 2 个后台线程。 1. 这是怎么发生的? 2. 我的设置是否意味着我的网站不会处理并发请求?
这是我网站的一些配置
httpd.conf
WSGIScriptAlias / /var/www/NiceHouse/trunk/apache/django.wsgi
WSGISocketPrefix run/wsgi
<VirtualHost *:80>
WSGIDaemonProcess site-1 user=**** group=**** threads=1
WSGIProcessGroup site-1
ServerName *****
ServerAlias *****
ServerAdmin *****
#DocumentRoot /usr/local/www/documents
#Alias /robots.txt /usr/local/www/documents/robots.txt
#Alias /favicon.ico /usr/local/www/documents/favicon.ico
Alias /media/ /usr/lib/python2.4/site-packages/django/contrib/admin/media/
Alias /site_media/ /var/www/NiceHouse/trunk/media/
Alias /phpmyadmin /var/www/phpmyadmin/
#<Directory /usr/local/www/documents>
#Order allow,deny
#Allow from all
#</Directory>
WSGIScriptAlias / /var/www/NiceHouse/trunk/apache/django.wsgi
#<Directory /usr/local/www/wsgi-scripts>
#Order allow,deny
#Allow from all
#</Directory>
</VirtualHost>
swtune.conf
<IfModule prefork.c>
StartServers 1
MinSpareServers 1
MaxSpareServers 3
ServerLimit 50
MaxClients 50
MaxRequestsPerChild 1000
</IfModule>
django.wsgi
import sys
import os
os.environ['PYTHON_EGG_CACHE']='/tmp/hoge'
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
sys.path.insert(0,'/var/www/NiceHouse/trunk')
import settings
import django.core.management
django.core.management.setup_environ(settings)
utility = django.core.management.ManagementUtility()
command = utility.fetch_command('runserver')
command.validate()
import django.conf
import django.utils
django.utils.translation.activate(django.conf.settings.LANGUAGE_CODE)
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
我将不胜感激任何帮助
谢谢,Elad
First of all my site is up and running OK. There is no critical issues.
I want to understand a couple of things though.
I'll start with an overview of my system.
It's a django-powered site located on a CentOS 5.3 VPS with 256MB RAM, under apache with mod_wsgi.
The django application runs as a Daemon process with 1 threads.
What I need in my application is:
1. Initialize logging (currently it is working, but I get double logging every time)
2. Start to daemon threads to do some background work
Now, I have read and implemented the solution offered in
http://blog.dscpl.com.au/2010/03/improved-wsgi-script-for-use-with.html
but it didn't help much.
Basically I had to manually disable worker MPM (only prefork MPM is running) and configure the daemon process to be with one process and one thread (for some reason it does not work with any other combination).
But what is weird is that the site is running and it is creating the 2 background threads.
1. How does that happen?
2. Does my settings mean that concurrent requests will not be handled on my site?
Here is some configuration from my site
httpd.conf
WSGIScriptAlias / /var/www/NiceHouse/trunk/apache/django.wsgi
WSGISocketPrefix run/wsgi
<VirtualHost *:80>
WSGIDaemonProcess site-1 user=**** group=**** threads=1
WSGIProcessGroup site-1
ServerName *****
ServerAlias *****
ServerAdmin *****
#DocumentRoot /usr/local/www/documents
#Alias /robots.txt /usr/local/www/documents/robots.txt
#Alias /favicon.ico /usr/local/www/documents/favicon.ico
Alias /media/ /usr/lib/python2.4/site-packages/django/contrib/admin/media/
Alias /site_media/ /var/www/NiceHouse/trunk/media/
Alias /phpmyadmin /var/www/phpmyadmin/
#<Directory /usr/local/www/documents>
#Order allow,deny
#Allow from all
#</Directory>
WSGIScriptAlias / /var/www/NiceHouse/trunk/apache/django.wsgi
#<Directory /usr/local/www/wsgi-scripts>
#Order allow,deny
#Allow from all
#</Directory>
</VirtualHost>
swtune.conf
<IfModule prefork.c>
StartServers 1
MinSpareServers 1
MaxSpareServers 3
ServerLimit 50
MaxClients 50
MaxRequestsPerChild 1000
</IfModule>
django.wsgi
import sys
import os
os.environ['PYTHON_EGG_CACHE']='/tmp/hoge'
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
sys.path.insert(0,'/var/www/NiceHouse/trunk')
import settings
import django.core.management
django.core.management.setup_environ(settings)
utility = django.core.management.ManagementUtility()
command = utility.fetch_command('runserver')
command.validate()
import django.conf
import django.utils
django.utils.translation.activate(django.conf.settings.LANGUAGE_CODE)
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I'll appreciate any help
Thanks, Elad
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于多种原因,您可能会出现双重记录。首先是您将日志记录放在哪里?如果您卡在 settings.py 文件中进行日志记录,那么它将使用您正在使用的 WSGI 脚本文件内容执行两次。该博客文章解释了如何导入设置文件两次。
顺便说一句,您甚至没有正确复制 WSGI 脚本文件。博客文章中没有设置 DJANGO_SETTINGS_MODULE 环境变量。
如果您自己通过两个不同的路径导入带有日志记录的模块,您也可能会看到双重日志记录。即,按站点包名称限定和不限定。根据记忆,这个问题在博客文章中也提到过。
关于后台线程,虽然您指定了threads=1,但mod_wsgi有一些自己的独立线程,以确保一切正常工作,并在认为进程死锁时终止进程。
顺便说一句,如果你不使用 mod_php,你真的应该让工人 MPM 运行。使用 prefork MPM 只会浪费内存。
You may be getting double logging for a number of reasons. First is where have you put the logging? If you have stuck logging in settings.py file, then it will get executed twice with that WSGI script file contents you are using. The blog post explains how settings file is imported twice.
BTW, you didn't even copy the WSGI script file correctly. That in the blog post does not set DJANGO_SETTINGS_MODULE environment variable.
You may also be seeing double logging if you yourself are importing a module with the logging in it via two different paths. That is, qualified and not qualified by site package name. From memory this problem is also mention in blog post.
In respect of background threads, although you have specified threads=1, mod_wsgi has a few separate threads of its own to make sure things are working correctly and kill off process if deemed it is dead locked.
BTW, you really should get worker MPM going if you aren't using mod_php. You are only going to waste memory by using prefork MPM.