Hello!
I recently switched my Django environment from using Apache to Gunicorn. After this switch we noticed that our middleware was being called twice for each request. To confirm my suspicion I wrote a new piece of middleware:
class TemporaryMiddleware(object):
def process_request(self, request):
if not hasattr(request, '_secret'):
request._secret = uuid.uuid4()
logging.info("set secret to {0}".format(request._secret))
else:
logging.info(request._secret)
def process_response(self, request, response):
logging.info("response {0}{1}".format(request._secret, response.status_code))
return response
After registering my TemporaryMiddleware I headed back to the logs:
INFO 2014-03-19 09:32:23,397 middleware.py:26] set secret to bb89e0ab-a30b-42ce-800b-7129a3b323ae
INFO 2014-03-19 09:32:23,398 middleware.py:28] bb89e0ab-a30b-42ce-800b-7129a3b323ae
INFO 2014-03-19 09:32:23,841 middleware.py:32] response bb89e0ab-a30b-42ce-800b-7129a3b323ae200
INFO 2014-03-19 09:32:24,126 middleware.py:32] response bb89e0ab-a30b-42ce-800b-7129a3b323ae200
If the only change I make is to start the server using:
python manage.py runserver
Instead of:
python manage.py run_gunicorn
my logs say:
INFO 2014-03-19 09:48:25,325 middleware.py:26] set secret to 84b011f9-1689-4b2f-8202-01840c249937
INFO 2014-03-19 09:48:25,842 middleware.py:33] response 84b011f9-1689-4b2f-8202-01840c249937200
It seems to me that every request is being processed twice. This happens regardless of the number of threads or workers available, and when I run with more than 1 worker and/or more than one thread the process and thread id's for all four logging statements are the same. So I believe one thread is running through all of my middleware twice per request.
Has anyone seem anything like this before, or have any idea what might be going on? I've only been Djangoing for ~6 months and sometimes configuration stuff still escapes me.
Thanks,
Tyler Bettilyon