2011-09-28 104 views
1

一些技术规格:的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} 

感谢您的时间

+0

尝试在GET请求中添加时间戳变量。 – spicavigo

+0

我试过这个,不起作用我认为是服务器端 – dancio

+1

[uwsgi nginx模块](http://wiki.nginx.org/HttpUwsgiModule)有几个控制缓存的指令。这不太可能是你的问题,据我所知默认情况下缓存没有启用。 – zeekay

回答

0

你尝试过禁用蟒蛇优化器(请从uWSGI配置文件的优化选项)?

即使它看起来更多的是JS/HTML /客户端的问题一些对象可以使某种启用优化乱。请不要跟随愚蠢的劝告降级到不支持超过1年的版本。

+0

我已经从uWSGI配置文件中删除了优化器并清除了所有* .pyo,* .pyc文件,但是没有任何更改 – dancio

+0

因此它确实存在缓存问题。你可以尝试把uWSGI放在apache而不是nginx之后吗? (你可以在没有问题的情况下沿mod_wsgi添加mod_uwsgi)。通过这种方式,我们可以了解问题所在。 – roberto

+0

...另一个测试你可以通过curl直接询问nginx的json视图,所以你可以确定浏览器缓存不会发挥作用。你有没有尝试在嵌入式和守护进程模式下使用mod_wsgi? damon_mode更类似于uWSGI方法 – roberto