Note that there are some explanatory texts on larger screens.

plurals
  1. PONginx + uWsgi + Django strange json response behavior
    text
    copied!<p>Some tech specifications:</p> <ul> <li>CentOS 6.0</li> <li>uWSGI 0.9.9.2</li> <li>Nginx 1.0.5</li> <li>Django 1.3.1</li> </ul> <p>uWSGI:</p> <pre><code> [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 </code></pre> <p>JSON serializer:</p> <pre><code>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) </code></pre> <p>JSON HttpResponse:</p> <pre><code>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) </code></pre> <p>I have a few subclasses of JsonResponse with other json_status_code and message.</p> <p>View:</p> <pre><code>.... if application.status == Application.STATUS_REMOVED: return JsonApplicationSuspendedResponse() .... return JsonResponse() </code></pre> <p><em><strong>PROBLEM:</em></strong></p> <p>Even when the application status is changing it happens that I receives old json lest say for 3 - 4 seconds and then it returning JsonApplicationSuspendedResponse() correctly.</p> <p>I checked the database application status update takes place immediately, also noticed that if I reboot uWSGI and send request response is correct and the opposite situation happens. Second request after status change can have old json.</p> <p>It looks as if they write the response for few sencods and had a problem with her ​​refresh (Cache is disabled).</p> <p>Any ideas where it might be the problem ?</p> <p>The same code works fine on Apache2 and mod_wsgi</p> <p><strong>fixed</strong></p> <p>This was a really stupid bug, in JsonResponse I had:</p> <pre><code>def __init__(self, json={}, *args, **kwargs): </code></pre> <p>part <strong>json={}</strong> is quite important here, JsonResponse and each subclass of JsonResponse after init shared initial dict and its contents, so the answer looked like a did not change.</p> <pre><code>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} </code></pre> <p><strong>Thanks for your time</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload