[Fixed]-Django View Returns Multiple Results Upon Refresh

1👍

How do you start and stop your uWSGI server might be an issue here.

uWSGI is not reloading itself when code changes by default, so unless stopped or reloaded, it will contain cached version of old django code and operate with it. But caching won’t occur immediately, just on first request.

But… uWSGI can spawn multiple workers at once, so it can process multiple requests at once (each worker thread can process one request at time) and that workers will have their own cached version of code.

So one scenario can be: you’ve started you uWSGI server, then you did some request (before making any views, so standard django it worked shows up), that time one of workers cached code responsible for that action. You’ve decided to change something, but you’ve done some error, on next request code providing to that error was cached on another worker. Then you’ve fixed that error and next worker cached fixed code, providing to you your hello world response.

And now there is situation when all workers have some cached version of your code and depending of which one will process your code, you are getting different results.

Solution for that is: restart your uWSGI instance, and maybe add py-auto-reload to your uWSGI config, so uWSGI will automatically reload when code is changed (use that option only in development, never in production environment).

Other scenario is: you don’t have multiple workers, but every time you’ve changed something in code, you’re starting new uWSGI instance without stopping old one. That will cause multiple instances to run in same time, and when you are using unix socket, they will co-exist, taking turns when processing requests. If it’s the case, stop all uWSGI instances and next time stop old instance before starting new. Or just simply reload old one.

Leave a comment