0

Working on a middleware that slows down dev server response time. To simplify it I've created a very simple example that measures get_response time and then sleeps so the whole request cycle takes approximately 2 seconds.

The problem is that when there are multiple requests sent at once, some of them sleep 2xN time (not just a little bit longer, it's multiplicated).

I assume it's because of limits of Django's dev server concurrency. Is it possible to make it work?

class SnailMiddleware:
    """ Middleware that throttle responses """
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request: HttpRequest):
        start = now()
        response = self.get_response(request)
        elapsed_time = (now() - start).total_seconds()
        time.sleep(2 - elapsed_time)
        return response

These are the times:

enter image description here

Do you know where is the problem and how to make it work?

Milano
  • 18,048
  • 37
  • 153
  • 353
  • Django works by default synchronic, that means that it processes one request at a time. Usually one works with a webserver that runs different instances of the Django web app, and does proper load balancing to process *multiple* requests *simultanously*. – Willem Van Onsem Aug 29 '21 at 10:37
  • @WillemVanOnsem Thank you! Do you have any idea on how to make this work (if even possible)? – Milano Sep 04 '21 at 17:33

0 Answers0