显示标签为“python”的博文。显示所有博文
显示标签为“python”的博文。显示所有博文

星期六, 十月 27, 2007

[Django]Windows下Django配置Apache示范设置

[Django]Windows下Django配置Apache示范设置
Version
Date
Creator
Description
1.0.0.1
2006-11-20
郑昀
草稿

继续阅读之前,我们假设您熟悉以下知识:
n Python
n Django
n Apache
如果不熟悉这些知识点,可以看本文最后资源列表中的文章。
本文讨论了在Windows环境下,将Django配置到Apache Web Server时所遇到的问题,以及最终解决的示范步骤。

您可以遵循后文描述的配置步骤,在此之前,我们先描述两个常见的错误现象。
[常见错误现象1]
关键词: client denied by server configuration。
表象: 访问了http://localhost/mysite/ 后,假如发现图片没有加载,或者css没有应用上,并且httpd.conf文件中Location的配置绝对路径肯定没错。

解释:
第一, 首先检查httpd.conf配置Alias的配置,
看看Alias设置的路径是否正确,
Alias /site_media c:/django/myproject/media
请保证您附加的这个路径肯定是你的图片或者css存放的路径。
其次,请检查您的django项目myproject目录下的settings.py,保证MEDIA_ROOT、STATIC_PATH、TEMPLATE_DIRS这三个参数指向的绝对路径正确无误。
最后,如果上面的参数都没错,却还是不行。那么请您检查Apache的日志文件
Apache2.2\logs\error.log
如果您发现有这样的错误提示:
错误日志
[Mon Nov 20 17:27:08 2006] [notice] Child 4172: Starting thread to listen on port 80.
[Mon Nov 20 17:27:08 2006] [error] [client 127.0.0.1] client denied by server configuration: c:/Django/myproject/media/css/global.css, referer: http://localhost:80/mysite/
那么说明是因为对这些资源文件的访问被拒绝了。
此时,请您浏览 http://localhost/site_media/css/global.css ,如果遇到了HTTP 403错误,就说明是权限问题。
此时,请到httpd.conf,增加如下配置来允许静态资源文件夹被访问:
增加的配置行
# 对需要访问的区域,可以增加正确的Directory块
# 否则会得到这样的错误:client denied by server configuration: c:/Django/myproject/media/css/global.css, referer: http:/ /localhost:80/mysite/

Order Deny,Allow
Allow from all

重启Apache服务。

[常见错误现象2]
关键词: EnvironmentError: Could not import settings。
表象: 访问了http://localhost/mysite/ 后,直接页面报告如下错误:
页面错误输出
1. Mod_python error: “PythonHandler django.core.handlers.modpython”
Traceback (most recent call last):
File “C:\Python24\Lib\site-packages\mod_python\apache.py”, line 299, in HandlerDispatchresult = object(req)
File “c:\django_src\django\core\handlers\modpython.py”, line 163, in handlerreturn ModPythonHandler()(req)
File “c:\django_src\django\core\handlers\modpython.py”, line 125, in __call__if settings.ENABLE_PSYCO:
File “c:\django_src\django\conf\__init__.py”, line 27, in __getattr__self._import_settings()
File “c:\django_src\django\conf\__init__.py”, line 54, in _import_settingsself._target = Settings(settings_module)
File “c:\django_src\django\conf\__init__.py”, line 82, in __init__raise EnvironmentError, “Could not import settings ‘%s’ (Is it on sys.path? Does it have syntax errors?): %s” % (self.SETTINGS_MODULE, e)
EnvironmentError: Could not import settings ‘myproject.settings’ (Is it on sys.path? Does it have syntax errors?): No module named myproject.settings

这是开始配置django+apache最容易遇到的问题,:D。

解释:
这是因为apache在系统目录下找不到myproject/settings.py文件。
首先检查PythonPath设置的路径是否正确:
PythonPath "sys.path+['c:/django']"
注意,这个'c:/django'路径实际是我们的项目路径c:/django/myproject的上一级目录!不要写错了。
增加的配置行
# mysite目录路径: c:/django/myproject,
# 但是对于PythonPath,必须设置成这个目录的上一级目录!
# this site url:http://localhost:80/mysite/

SetHandler python-program
PythonPath "sys.path+['c:/django']"
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE myproject.settings
PythonInterpreter mysite
PythonDebug On


配置Django到Apache的步骤:
如何在Windows环境下安装Apache和mod_Python, 这篇Blog解释得很明白,我下面的这些步骤得到了验证。1:从 http://httpd.apache.org/来获取apache_2.2.3-win32-x86-no_ssl.msi这个安装文件;运行这个msi即可顺利安装Apache。
2:
网络上有很多说可以利用Apache和mod_python源代码来编译安装mod_python的,但是如果你仅仅想快速安装,那么请从
http://www.apache.org/dist/httpd/modpython/win/3.2.10/
直接下载
mod_python-3.2.10.win32-py2.4-apache2.2.exe
文件,运行这个exe即可顺利把mod_python安装到Python2.4以及Apache2.2;
由于GFW的封锁,可能您无法访问apache网站,那么可以从这里下载:
http://www.cnblogs.com/Files/zhengyun_ustc/mod_python-3.2.10.win32-py2.4-apache2.2.rar
3:
我们用
Python django-admin.py startproject myproject
命令,在C盘的django目录下创建了一个Django项目myproject。我们把它引用的图片文件和css文件等静态资源都放在myproject文件夹下的media目录中。
此时,将myproject目录下的settings.py文件中的这三个参数修改一下,主要是为了从相对路径变成绝对路径:
修改的配置行
# Django settings for myproject project.
'''
将相对路径改为绝对路径。主要有:
MEDIA_ROOT
TEMPLATE_DIRS
STATIC_PATH
'''
# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = 'c:/django/myproject'
STATIC_PATH = 'c:/django/myproject/media'
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates".
# Always use forward slashes, even on Windows.
'c:/django/myproject/templates',
)
切记切记,一定要保证这三个参数是绝对路径。
4:
配置Apache的httpd.conf配置文件:
首先,在“Dynamic Shared Object (DSO) Support”的配置下增加一行
LoadModule python_module modules/mod_python.so
这个必须手动添加。
5:
我们列出此时Apache所需要的参数分别为:
项目名:myporject
试图访问的URL为:http://localhost/mysite/
静态资源文件的存放目录:c:/django/myproject/media
项目文件夹路径:c:/django/myproject
好了,此时你就应该在httpd.conf文件的最后附加这段配置,以便让Apache知道到哪里去定位myproject/settings.py文件:
增加的配置行
# mysite目录路径: c:/django/myproject,
# 但是对于PythonPath,必须设置成这个目录的上一级目录!
# this site url:http://localhost:80/mysite/

SetHandler python-program
PythonPath "sys.path+['c:/django']"
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE myproject.settings
PythonInterpreter mysite
PythonDebug On

对于上面的配置,Limodou注释道:“上面 PythonPath 主要是将 myproject的目录加入到 sys.path ,以便 Django 可以找到。需要使用绝对路径。
SetEvn 中设置的 DJANGO_SETTINGS_MODULE 就对应于你的 项目名.配置文件。因此为了能导入 项目名.配置文件,就需要前面的 PythonPath 的设置。
PythonDebug 和 PythonAutoReload 建议在生产时设为 Off 。”
为了让图片、css、script能够被Apache成功加载,还需要在httpd.conf最后附加这段配置:
增加的配置行
#Alias /site_media 是用来将 myproject的静态文件设置一个 URL 访问的别名。
Alias /site_media c:/django/myproject/media

SetHandler None


#Alias /media 是将 Django Admin 的静态文件设置一个 URL 的访问别名。
Alias /media c:/Django-0.95/django/contrib/admin/media

SetHandler None


# file types we want to serve statically
# case insensative match

SetHandler None


Limodou在Step by step中特地说“同时可以注意到 settings 我改为了 settings_apache 了。一方面将要把其中的内容有关相对路径的东西改为绝对路径,另一方面我还想保持现在的 settings.py 。”这也是一个很好的做法。
最后,修改MaxRequestsPerChild为1,这是一个可选项。Limodou是这么说的“同时如果你不想每次重启Apache 来进行测试,可以将: MaxRequestsPerChild 0. 改为: MaxRequestsPerChild 1”

重启Apache服务。
6:
浏览 http://localhost/mysite/ 即可。

[参考资料]
1:《How to use Django with mod_python》Django官方文档
2:《HOWTO: Django on Windows》这篇blog给了我很大的帮助
3:《Django Step by Step (十二)》limodou的帮助文档4:《如何安装 Django》

星期二, 十月 10, 2006

mod_lua性能提升,是mod_python速度115%

在上次的性能对比之后,忍不住对mod_lua的程序作了改进,下面是最新的测试结果

mod_python
This is ApacheBench, Version 2.0.41-dev <$Revision: 1.121.2.12 $> apache-2.0
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 2006 The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)


Server Software: Apache/2.0.59
Server Hostname: localhost
Server Port: 80

Document Path: /tmp/hello.py
Document Length: 12 bytes

Concurrency Level: 1
Time taken for tests: 1.78125 seconds
Complete requests: 500
Failed requests: 0
Write errors: 0
Total transferred: 102500 bytes
HTML transferred: 6000 bytes
Requests per second: 463.77 [#/sec] (mean)
Time per request: 2.156 [ms] (mean)
Time per request: 2.156 [ms] (mean, across all concurrent requests)
Transfer rate: 92.75 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 2.3 0 15
Processing: 0 1 4.7 0 15
Waiting: 0 1 4.5 0 15
Total: 0 1 5.1 0 15

Percentage of the requests served within a certain time (ms)
50% 0
66% 0
75% 0
80% 0
90% 15
95% 15
98% 15
99% 15
100% 15 (longest request)
mod_lua
This is ApacheBench, Version 2.0.41-dev <$Revision: 1.121.2.12 $> apache-2.0
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 2006 The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)


Server Software: Apache/2.0.59
Server Hostname: localhost
Server Port: 80

Document Path: /tmp/hello.lua
Document Length: 12 bytes

Concurrency Level: 1
Time taken for tests: 0.937500 seconds
Complete requests: 500
Failed requests: 0
Write errors: 0
Total transferred: 102000 bytes
HTML transferred: 6000 bytes
Requests per second: 533.33 [#/sec] (mean)
Time per request: 1.875 [ms] (mean)
Time per request: 1.875 [ms] (mean, across all concurrent requests)
Transfer rate: 105.60 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 2.2 0 15
Processing: 0 1 4.2 0 15
Waiting: 0 1 4.1 0 15
Total: 0 1 4.7 0 15

Percentage of the requests served within a certain time (ms)
50% 0
66% 0
75% 0
80% 0
90% 15
95% 15
98% 15
99% 15
100% 15 (longest request)
这次修改可谓全胜
速度比如下每秒处理的请求 463.77/533.33(mod_python/mod_lua)
每个请求处理花费的时间微妙: 2.156/1.875(mod_python/mod_lua)

性能提升的原因说明:

1 mod_lua从串行处理方式改成并行处理方式
2 lua虚拟机采用了空闲池处理

星期六, 十月 07, 2006

编成语言性能评测

Ruby, Io, PHP, Python, Lua, Java, Haskell, and Plain C Fractal Benchmark

http://www.timestretch.com/FractalBenchmark.html

compare python vs lua, ruby, PHP and Haskell

http://william-os4y.livejournal.com/2216.html


 

星期五, 十月 06, 2006

几种Apache CGI模块性能简单分析比较

最近对  mod_python, mod_php, mod_ruby, mod_lua做一个简单的性能比较,目的是想知道mod_lua在性能方面该如何改进!

工作在Windows 2003, VS 2005下完成,全部采用动态编译
-
mod_python已被捐赈给apache由apache关方维护,轻松搞定(支持python 2.4,不支持python2.5)
mod_php 一直被作为CGI开发的热点,也非常容易搞定
mod_lua 是本人的开源项目,自然不用碰到问题(支持最新lua 5.1.1)
mod_ruby 这个小日本的东西,在他上面花的时间最多, 首先与yarv(ruby 2.0)还不兼容,另外在ruby 1.8.5的编译时候需要在程序内部切换一个宏,另外还有个内存问题! 文档资料也很少.(mod_lua根本还没有文档,我以后会陆续给出)!需要一定的经验,不能自动完成.

( 就我对apache与ruby的了解程度自信可以搞定, 对于小日本开源软件的态度还真不敢恭维,ROR不知是被那位炒起来的, 反正mod_ruby的性能不敢恭维.)

---PHP的测试
页面
echo "Hello World!";
命令行
ab -n 100 http://localhost/tmp/hello.php

输出结果
This is ApacheBench, Version 2.0.41-dev <$Revision: 1.121.2.12 $> apache-2.0
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 2006 The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient).....done


Server Software: Apache/2.0.59
Server Hostname: localhost
Server Port: ; &nbsp; 80

Document Path: &nbsp; ; /tmp/hello.php
Document Length: 12 bytes

Concurrency Level: &nbsp; &nbsp; 1

Time taken for tests: 0.609375 seconds
Complete requests: 100
Failed requests: 0
Write errors: 0
Total transferred: 28700 bytes
HTML transferred: 1200 bytes
Requests per second: 164.10 [#/sec] (mean)
Time per request: 6.094 [ms] (mean)
Time per request: 6.094 [ms] (mean, across all concurrent requests)
Transfer rate: 45.95 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 2.1 0 15
Processing: 0 4 7.2 0 15
Waiting: 0 4 7.1 0 15
Total: 0 5 7.2 0 15

Percentage of the requests served within a certain time (ms)
50% 0
66% 15
75% 15
80% 15
90% 15
95% 15
98% 15
99% 15
100% 15 (longest request)

------mod_pythonde的测试
页面
from mod_python import apache

def handler(req):
req.content_type = 'text/plain'
req.write("Hello World!")
return apache.OK
命令行
ab -n 100 http://localhost/tmp/hello.py
输出结果
This is ApacheBench, Version 2.0.41-dev <$Revision: 1.121.2.12 $> apache-2.0
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 2006 The Apache Software Foundation, http://www.apache.org/

Benchmarking www.nutstech.com (be patient).....done


Server Software: Apache/2.0.59
Server Hostname: www.nutstech.com
Server Port: 80

Document Path: /tmp/hello.py
Document Length: 12 bytes

Concurrency Level: 1
Time taken for tests: 0.359375 seconds
Complete requests: 100
Failed requests: 0
Write errors: 0
Total transferred: 20500 bytes
HTML transferred: 1200 bytes
Requests per second: 278.26 [#/sec] (mean)
Time per request: 3.594 [ms] (mean)
Time per request: 3.594 [ms] (mean, across all concurrent requests)
Transfer rate: 55.65 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 2.6 0 15
Processing: 0 2 5.7 0 15
Waiting: 0 2 5.2 0 15
Total: 0 3 6.0 0 15

Percentage of the requests served within a certain time (ms)
50% 0
66% 0
75% 0
80% 15
90% 15
95% 15
98% 15
99% 15
100% 15 (longest request)

-----mod_lua的测试
页面程序
http.print("Hello World!")
命令行
ab -n 100 http://localhost/tmp/hello.lua
测试结果
This is ApacheBench, Version 2.0.41-dev <$Revision: 1.121.2.12 $> apache-2.0
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 2006 The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient).....done


Server Software: Apache/2.0.59
Server Hostname: localhost
Server Port: 80

Document Path: /tmp/hello.lua
Document Length: 12 bytes

Concurrency Level: 1
Time taken for tests: 0.468750 seconds
Complete requests: 100
Failed requests: 0
Write errors: 0
Total transferred: 24200 bytes
HTML transferred: 1200 bytes
Requests per second: 213.33 [#/sec] (mean)
Time per request: 4.688 [ms] (mean)
Time per request: 4.688 [ms] (mean, across all concurrent requests)
Transfer rate: 49.07 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 2.6 0 15
Processing: 0 3 6.6 0 31
Waiting: 0 2 6.0 0 15
Total: 0 3 6.9 0 31

Percentage of the requests served within a certain time (ms)
50% 0
66% 0
75% 0
80% 15
90% 15
95% 15
98% 15
99% 31
100% 31 (longest request)

----mod_ruby的测试
页面
#!/usr/bin/ruby

require "cgi"

cgi = CGI.new
print cgi.header("type"=>"text/plain")
print "Hello World!"


命令
ab -n 100 http://localhost/tmp/hello.rbx
测试结果
 This is ApacheBench, Version 2.0.41-dev <$Revision: 1.121.2.12 $> apache-2.0
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 2006 The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient).....done


Server Software: Apache/2.0.59
Server Hostname: localhost
Server Port: 80

Document Path: /tmp/hello.rbx
Document Length: 12 bytes

Concurrency Level: 1
Time taken for tests: 1.187500 seconds
Complete requests: 100
Failed requests: 0
Write errors: 0
Total transferred: 24300 bytes
HTML transferred: 1200 bytes
Requests per second: 84.21 [#/sec] (mean)
Time per request: 11.875 [ms] (mean)
Time per request: 11.875 [ms] (mean, across all concurrent requests)
Transfer rate: 19.37 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 2.1 0 15
Processing: 0 10 9.1 15 46
Waiting: 0 9 9.1 15 46
Total: 0 10 9.1 15 46

Percentage of the requests served within a certain time (ms)
50% 15
66% 15
75% 15
80% 15
90% 15
95% 31
98% 31
99% 46
100% 46 (longest request)

This is ApacheBench, Version 2.0.41-dev <$Revision: 1.121.2.12 $> apache-2.0
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 2006 The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient).....done


Server Software: Apache/2.0.59
Server Hostname: localhost
Server Port: 80

Document Path: /tmp/hello.rbx
Document Length: 12 bytes

Concurrency Level: 1
Time taken for tests: 1.187500 seconds
Complete requests: 100
Failed requests: 0
Write errors: 0
Total transferred: 24300 bytes
HTML transferred: 1200 bytes
Requests per second: 84.21 [#/sec] (mean)
Time per request: 11.875 [ms] (mean)
Time per request: 11.875 [ms] (mean, across all concurrent requests)
Transfer rate: 19.37 [Kbytes/sec] received


Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 2.1 0 15
Processing: 0 10 9.1 15 46
Waiting: 0 9 9.1 15 46
Total: 0 10 9.1 15 46

Percentage of the requests served within a certain time (ms)
50% 15
66% 15
75% 15
80% 15
90% 15
95% 31
98% 31
99% 46
100% 46 (longest request)



结果比较:


  • 模块名称 结果(次/秒)
  • mod_python 278.26
  • mod_lua 213.33
  • mod_php 164.10
  • mod_ruby 84.21
mod_python的处理速度果然快, 难怪google实用了很多的python, 不要犹豫, 用python吧!
当然php在WEB开发中的地位已经巩固, 但我相信很快就会被python赶上并超越
我开发的mod_lua,是为嵌入式应用准备的,在执行速度与模块大小方面都不错!且可直接用共享内存来通信 具体大家可以到 http://sf.net/projects/modlua 去了解!
mod_ruby是在是不敢恭维,如果采用eruby估计会更慢,将用ruby开发web的人,需要仔细考虑了


星期三, 十月 04, 2006

从微尘《一个Python程序的6次改进》想到的

一个Python程序的6次改进 在 http://blog.csdn.net/tinydust 贴出

下面是总结出来的最快的python实现
import datetime

if __name__ == "__main__":
    t1=datetime.datetime.today()
    f = file("email.txt","r")
    f2 = file("email_new.txt","w")
    f2.writelines(set(f))
    f.close()
    f2.close()
    t2=datetime.datetime.today()
    print str(t2-t1)

在我机器上的测试结果:
python 2.5:  0:00:00.172000
这个程序语言特性用到了极致, 近乎完美地解决了问题,佩服!

一位网友的ruby实现
#!/usr/bin/env ruby
t = Time.new

file = 'email.txt'

f1= File.new('email.txt', 'r')
data = f1.readlines
f1.close

f2 = File.new('email_rb.txt', 'w')
f2.write(data.uniq.join)
f2.close
puts Time.new - t

ruby 2.0.0 (Base: Ruby 1.9.0 2006-04-08) [i386-mswin32]
YARVCore 0.4.1 Rev: 545 (2006-09-01) [opts: [inline method cache] ]

这个程序也很酷,结果  0.391

看了之后,马上产生了用lua写一个同样程序的想法!

lines = {}

f1 = io.open ("email.txt","r")
for line in f1:lines() do lines[line] = true end
f1:close()

f2 = io.open ("e_t.txt","w+")
for line in pairs(lines) do f2:write(line, "\n") end
f2:close()
print(os.clock())

Lua 5.1.1 Copyright (C) 1994-2006 Lua.org, PUC-Rio: 0.406
测试结果: Python:Ruby:Lua = 172:391:406

是不是LUA就差了呢!才不是呢!

这也是可以理解的, LUA io操作肯定是没有优化的,没有一次将所有的数据读入/写出,我尝试一次读入出数据,然后对读入的数据按照string的方式来处理,然后一次写盘,测试的结果是比上面的程序慢了一倍,问题在于lua对于字符串的操作同样也为优化,花的时间很长,CPU/内存开销都很大,看来又比要对字符处处理做一个扩展库.
另外将上面的程序两个循环合并成一个同时操作读入/写出文件后性能也不理想,因为同时在两个文件上进行IO操作,硬盘磁头会频繁移动,性能反而降低。LUA的优势要嵌入C/C++才能表现出来!

不过python的表现让我惊奇, rite的表现也很惹人注意。

由于我的开发工作决定使用lua会更好。所以我会继续使用lua.

iolanguage也进入了我的视线,抽空也的研究一下!