一些技术规格:的Nginx + uWsgi + Django的奇怪的JSON响应行为
- 的CentOS 6.0
- uWSGI 0.9.9.2
- Nginx的1.0.5
- 的Django 1.3.1
uWSGI:
[uwsgi]
socket = 127.0.0.1:3031
master = true
processes = 5
uid = xx
gid = xx
env = DJANGO_SETTINGS_MODULE=xx.settings
module = django.core.handlers.wsgi:WSGIHandler()
post-buffering = 8192
harakiri = 30
harakiri-verbose = true
disable-logging = true
logto = /var/log/xx.log
vacuum = true
optimize = 2
JSON序列:
class LazyEncoder(simplejson.JSONEncoder, json.Serializer):
def default(self, obj):
if isinstance(obj, Promise):
return force_unicode(obj)
if isinstance(obj, Decimal):
u_value = force_unicode(obj)
if u'.' in u_value:
return float(u_value)
return int(u_value)
return super(lazy_encoder, self).default(obj)
JSON的HttpResponse:
class JsonResponse(HttpResponse):
status_code = 200
json_status_code = 200
message = _('OK')
def __init__(self, json={}, *args, **kwargs):
mimetype = kwargs.pop('mimetype', 'application/json')
if not 'status' in json:
json['status'] = {'code': self.json_status_code, 'message': self.message}
super(JsonResponse, self).__init__(LazyEncoder(indent=settings.DEBUG and 4 or None, separators=settings.DEBUG and (', ', ': ') or (',', ':')).encode(json), mimetype=mimetype, *args, **kwargs)
我JsonResponse与其他json_status_code和消息的几个子类。
查看:
....
if application.status == Application.STATUS_REMOVED:
return JsonApplicationSuspendedResponse()
....
return JsonResponse()
问题:
即使应用现状正在改变它发生,我收到老JSON免得说3 - 4秒钟,然后再使其恢复JsonApplicationSuspendedResponse( )正确。
我检查数据库应用程序状态立即更新发生, 也注意到,如果我重新启动uWSGI并发送请求响应是正确的,相反的情况发生。状态改变之后的第二个请求可以具有旧的json。
看起来好像他们写了几sencods响应,并有一个问题,她刷新(Cache是禁用)。
任何想法,它可能是什么问题?
相同的代码工作正常上的Apache2和mod_wsgi的
固定
这是一个非常愚蠢的错误,在JsonResponse我:
def __init__(self, json={}, *args, **kwargs):
部分JSON = {}是很重要的位置,JsonResponse和init共享初始字典和其内容的JsonResponse的每个子类,所以答案看上去就像一个没有改变。
def __init__(self, json=None, *args, **kwargs):
mimetype = kwargs.pop('mimetype', 'application/json')
if not json:
json = {}
if not 'status' in json:
json['status'] = {'code': self.json_status_code, 'message': self.message}
感谢您的时间
尝试在GET请求中添加时间戳变量。 – spicavigo
我试过这个,不起作用我认为是服务器端 – dancio
[uwsgi nginx模块](http://wiki.nginx.org/HttpUwsgiModule)有几个控制缓存的指令。这不太可能是你的问题,据我所知默认情况下缓存没有启用。 – zeekay