Re: [python] [django] [mod_pyth on] [apache] 再次质疑Django+Mod_python 时对环境变量的处理
"憨狗" <[email protected]>
| Newsgroups | gmane.comp.python.chinese |
|---|---|
| Message-ID | <[email protected]> |
> 这个并不自相矛盾,SetEnv不是mod_python处理的,而是mod_env处理的,处理的结果就在req.subprocess_env,只是mod_python并没有赋值给os.environ,所以Django要自己搞一下。问题就在于os.environ.update的时候还putenv了,一般来讲修改过环境变量要恢复回去,Django没有干这个事。给一个小补丁来干这个:
>
mod_env和mod_python如何处理environ我没有详查,但是经过测试:
未添加setenv时os.path.expanduser("~/")拿到的结果是None
setenv HOEM /home/hackgou之后,os.path.expanduser("~/")拿到的结果是/root,而不是/home/hackgou。
而只有以hackgou用户通过sudo 启动apache时,可以得到正确结果,
此时再设置setenv HOME /home/hackgou,已经不重要了。
> @@ -141,6 +141,8 @@
>
> def __call__(self, req):
> # mod_python fakes the environ, and thus doesn't process SetEnv.
> This fixes that
> + # First save env, after finished restore
> + save_env = os.environ.copy()
> os.environ.update(req.subprocess_env)
>
> # now that the environ works we can see the correct settings, so
> imports
> @@ -166,6 +168,12 @@
> response = self.apply_response_fixes(request, response)
> finally:
> dispatcher.send(signal=signals.request_finished)
> + osenv = os.environ
> + # restore env
> + for k in osenv.keys():
> + del osenv[k]
> + for k in sav_env:
> + osenv[k] = sav_env[k]
>
> # Convert our custom HttpResponse object back into the mod_python
> req.
> req.content_type = response['Content-Type']
>
> 这样就可以把
> import os, pwd
> os.environ["HOME"] = pwd.getpwuid(os.getuid()).pw_dir
>
> 加到这里面,不违反DRY原则吧 ;-)
这样的patch似乎是比较完美的解决办法,破坏现场后恢复现场。我还没有测试过,
不过看其应该没有问题!Very Good!
不知道这个patch有提交么?
另外一个问题,在apache的worker和prefork模式下,是否会影响其他应用,
因为我的理解是worker模式下应该会比较困难,因为所有线程共享相同的系统环境变量共享,
所以我认为相互间肯定会有污染,而在prefork模式下,我就不确定了!
理想状态下,是不应该干扰的。
之所以有这个问题,是因为曾经有过这样的情况:
apache同时支持mod_python和mod_php时。
php中获取本地时间有时候是洛杉矶LA时间有时候是上海时间,
这个服务器在上海,而Django应用的时区需要设置为LA,这个时候就互相影响:
如果这个处理php的进程还没有处理过Django的请求,那么它得到的本地时间是上海时间
如果这个处理php的进程已经处理过Django的请求,那么它得到的本地时间就是LA时间
如果在PHP中设定时区的话,效果是一样的,也会互相干扰。
如果给Django打上这个patch,在prefork情况下,效果如何?
vcc有没有什么建议讲来听听?
_______________________________________________
python-chinese
Post: send [email protected]
Subscribe: send subscribe to [email protected]
Unsubscribe: send unsubscribe to [email protected]
Detail Info: http://python.cn/mailman/listinfo/python-chinese