Apache + mod_wsgi - 来自同一浏览器的并行请求
你能帮我解决一个问题吗?
我在用Apache 2.2.22和mod_wsgi。
我已经正确配置了WSGI,让它可以在多个线程上工作,并且使用的是Python。
我需要处理来自同一个浏览器的并行请求,但WSGI只能处理来自不同浏览器的并行请求(或者一个浏览器的一个标签页加一个隐身标签页)。
我尝试过嵌入式和守护进程模式。
Apache的配置如下:
WSGIDaemonProcess appname processes=5 threads=25 display-name=%{GROUP}
WSGIProcessGroup appname
WSGIScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
AddHandler wsgi-script .py
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
#Require all granted
</Directory>
我把wsgi.py放在/usr/lib/cgi-bin目录下,权限设置为755。
wsgi.py的内容是:
import os, sys
import time
from datetime import datetime
def application(environ, start_response):
sys.stderr.write("before wait time = %s\n" % str(datetime.now()))
sys.stderr.write("client= %s\n" % environ['REMOTE_ADDR'])
sys.stderr.write("waiting\n")
print >> sys.stderr, 'mod_wsgi.process_group = %s' % repr(environ['mod_wsgi.process_group'])
time.sleep(10)
sys.stderr.write("wait finished time = %s\n" % str(datetime.now()))
status = '200 OK'
output = 'Hello World!'
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
当我请求 http://my.ip/cgi-bin/wsgi.py 时,一切正常,但当我在同一个浏览器的两个标签页中并行运行时,第二个标签页在等待第一个完成...
日志信息:
[Tue Sep 02 18:25:06 2014] [error] before wait time = 2014-09-02 18:25:06.365133
[Tue Sep 02 18:25:06 2014] [error] client= 192.168.113.35
[Tue Sep 02 18:25:06 2014] [error] waiting
[Tue Sep 02 18:25:06 2014] [error] mod_wsgi.process_group = 'appname'
[Tue Sep 02 18:25:16 2014] [error] wait finished time = 2014-09-02 18:25:16.371944
[Tue Sep 02 18:25:16 2014] [error] before wait time = 2014-09-02 18:25:16.390348
[Tue Sep 02 18:25:16 2014] [error] client= 192.168.113.35
[Tue Sep 02 18:25:16 2014] [error] waiting
[Tue Sep 02 18:25:16 2014] [error] mod_wsgi.process_group = 'appname'
[Tue Sep 02 18:25:26 2014] [error] wait finished time = 2014-09-02 18:25:26.400464
1 个回答
1
我不会感到惊讶,如果这和浏览器在发送后续请求时重新利用已经打开的连接有关。如果从隐身标签页发送请求时,浏览器为这个请求打开了一个新的连接,那么服务器就可以用一个单独的线程来处理这个请求。这个情况正常工作,说明你的WSGI设置基本上是没问题的。