2011-09-28 54 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