问题
I got something unusual when developing a small web server using Python2.7+Apache+mod_wsgi. The main usage of this server is:
- Receiving regular requests from another server(server A), and put the request body into DB.
- Using some background threads to parse the request body in DB and send the parsed information to a third server(server B).
Apache is configured as Windows 'winnt' MPM mode. Code of dispatch.py, which is the entry point, is like below:
from urlparse import parse_qs
pool = MyThreadClass() # A customized thread class to parse request body in DB
pool.start()
def application(environ, start_response):
# Receiving regular request from server A and put request body into BD
output = 'OK'
start_response('200OK', [('Content-Type', 'text/plain')])
return [output]
At the beginning, the thread class is created when the server starts up and it runs perfectly. However, several hours later, I found the thread class (MyThreadClass) will be instantiated again, which means at that moment there will be two MyThreadClass instances running background.
I don't know if it's correct to create instance like this when Apache starts up. Do you get any ideas?
[edit1] Below is the config for wsgi part in apache:
WSGIScriptAlias / "E:/eclipse workspace/SubscriptionServer/src/business/dispatcher.py"
WSGIPythonPath "E:/eclipse workspace/SubscriptionServer/src"
<Directory "E:/eclipse workspace/SubscriptionServer">
Order deny,allow
Allow from all
</Directory>
[edit2] I have followed the instruction @Graham gave and set LogLevel to "info". I think I have find the cause but can't explain why!
Below are the logs for access log and error log. My server is listening to 8080. The first three lines of error.log are logged when server starts up. However, in access log at the time 16:36:18 2011, there is a call from 124.237.78.181 to request http://g.ha99y.com/R.asp?P=123.157.218.85:8080. Just at that time, in error log, the server load dispatcher.py again. I can't explain where does that call come from and why it creates two interpreters 'myhost.com:8080|' and 'myhost.com|'.
access.log:
124.237.78.181 - - [20/Dec/2011:16:36:18 +0800] "GET http://g.ha99y.com/R.asp?P=123.157.218.85:8080 HTTP/1.1" 404 29
error.log:
[Tue Dec 20 15:50:14 2011] [info] mod_wsgi (pid=1008): Create interpreter 'myhost.com:8080|'.
[Tue Dec 20 15:50:14 2011] [info] mod_wsgi (pid=1008): Adding 'E:/eclipse workspace/SubscriptionServer/src' to path.
[Tue Dec 20 15:50:14 2011] [info] [client 66.220.151.121] mod_wsgi (pid=1008, process='', application='myhost.com:8080|'): Loading WSGI script 'E:/eclipse workspace/SubscriptionServer/src/business/dispatcher.py'.
[Tue Dec 20 16:36:19 2011] [info] mod_wsgi (pid=1008): Create interpreter 'myhost.com|'.
[Tue Dec 20 16:36:19 2011] [info] mod_wsgi (pid=1008): Adding 'E:/eclipse workspace/SubscriptionServer/src' to path.
[Tue Dec 20 16:36:19 2011] [info] [client 124.237.78.181] mod_wsgi (pid=1008, process='', application='myhost.com|'): Loading WSGI script 'E:/eclipse workspace/SubscriptionServer/src/business/dispatcher.py'.
回答1:
You likely modified the WSGI script file which caused mod_wsgi to reload it.
Turn off reloading using:
WSGIScriptReloading Off
Whenever you make code changes then make sure you restart Apache.
Read:
http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode#Reloading_In_Embedded_Mode http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIScriptReloading
来源:https://stackoverflow.com/questions/8570446/variable-will-be-instantiated-more-than-once