变量将被实例化一次以上 [英] Variable will be instantiated more than once

查看:153
本文介绍了变量将被实例化一次以上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的 Python2.7 +的Apache + mod_wsgi的开发一个小型Web服务器时,有一些不寻常的。此服务器的主要用途是:


  1. 从另一台服务器(服务器A),接受正规的请求,并把请求体到数据库。

  2. 使用一些后台线程来解析在DB请求体,并发送分析的信息到第三个服务器(服务器B)。

Apache的配置为 Windows的WINNTMPM 模式。 dispatch.py​​的code,这是切入点,就像如下:

 从进口里urlparse parse_qs池= MyThreadClass()#自定义的线程类来解析请求主体在DB
pool.start()高清应用(ENVIRON,start_response):
    #接收来自服务器的定期请求,并把请求体到BD
    输出='OK'
    start_response('200 OK',[('内容类型','text / plain的')])
    返回[输出]

在开始的时候,当服务器启动时,它运行完美创建线程类。然而,几个小时后后,我发现线程类(MyThreadClass)将再次被实例化,这意味着在那一刻会有两个 MyThreadClass实例中运行的背景。结果
我不知道这是否是正确的,当阿帕奇启动以创建实例这样。你得到什么想法吗?

下面是在Apache中WSGI部分配置:

  WSGIScriptAlias​​ /E:/ Eclipse工作区/ SubscriptionServer / src目录/商务/ dispatcher.py
WSGIPythonPathE:/ Eclipse工作区/ SubscriptionServer / src目录<目录E:/ Eclipse工作区/ SubscriptionServer>
    为了否认,允许
    所有允许
< /目录>

我按照指令@Graham了,并设置loglevel改为信息。我想我已经找到了原因,但无法解释为什么!结果
以下是访问日志和错误日志中的日志。我的服务器监听8080当服务器启动error.log中的前三行被记录。然而,在当时的 16时36分18秒2011 访问日志,有一个从124.237.78.181调用请求的 http://g.ha99y.com/R.asp?P=123.157.218.85:8080 。就在那个时候,在错误日志,服务器的负载再次dispatcher.py。我无法解释,这是否呼叫来自,为什么它会创建两个相互preters'myhost.com:8080 |'和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.1404 29

error.log中:

  [星期二12月20日十五点50分十四秒2011] [信息]的mod_wsgi(PID = 1008):创建跨preTER'myhost.com:8080 |'。
[星期二12月20日15点50分十四秒2011] [信息]的mod_wsgi(PID = 1008):添加'E:/ Eclipse工作区/ SubscriptionServer / src'中设置路径。
[星期二12月20日15点50分十四秒2011] [信息] [客户66.220.151.121]的mod_wsgi(PID = 1008,工艺='',应用='myhost.com:8080 |'):加载WSGI脚本'E:/ Eclipse工作区/ SubscriptionServer / src目录/商业/ dispatcher.py。
[星期二12月20日16时36分十九秒2011] [信息]的mod_wsgi(PID = 1008):创建跨preTERmyhost.com |。
[星期二12月20日16时36分十九秒2011] [信息]的mod_wsgi(PID = 1008):添加'E:/ Eclipse工作区/ SubscriptionServer / src'中设置路径。
[星期二12月20日16时36分十九秒2011] [信息] [客户124.237.78.181]的mod_wsgi(PID = 1008,工艺='',应用='myhost.com |'):加载WSGI脚本'E:/ Eclipse工作区/SubscriptionServer/src/business/dispatcher.py。


解决方案

您可能会修改造成的mod_wsgi来重新加载它WSGI脚本文件。

关闭使用重装:

  WSGIScriptReloading关闭

无论什么时候code的变化,那么要确保重新启动Apache。

阅读:

http://$c$c.google.com/p/modwsgi/wiki/ReloadingSource$c$c#Reloading_In_Embedded_Mode
  的http://$c$c.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIScriptReloading

I got something unusual when developing a small web server using Python2.7+Apache+mod_wsgi. The main usage of this server is:

  1. Receiving regular requests from another server(server A), and put the request body into DB.
  2. 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'.

解决方案

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

这篇关于变量将被实例化一次以上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆