Why can Django handle multiple requests?

Viewed 4683

According to Django is synchronous or asynchronous. Django is synchronous. However I tested a blocking view with python manage.py runserver 8100:

    import time
    @action(detail=False, methods=['get'])
    def test(self, request):
        time.sleep(5)
        return {}

and triggered two requests with postman at 0s, 1s, and they returned at 5s, 6s. That seems not blocking/synchronous. Where am I wrong?

1 Answers

Even synchronous implementations handle usually multiple requests 'in parallel'.

They do so by using multiple processes, multiple threads or a mix of it.

Depending on the server they have a predefined (fixed) amount of processes or threads or they dynamically allocate threads or processes whenever another requests requires one.

An asynchronous server on the other hand can handle multiple requests 'in parallel' within only one thread / process.

The simple development server, that you can start with management.py runserver is using threading by default.

To best visualize this I suggest to change your code to:

import time
import os
import threading

@action(detail=False, methods=['get'])
def test(self, request):
    print("START PID", os.getpid(), "TID", threading.get_native_id())
    time.sleep(5)
    print("STOP  PID", os.getpid(), "TID", threading.get_native_id())
    return {pid=os.getpid(), tid=threading.get_native_id()}

As @xyres mentioned: There is a command line option to disable threading.

Just run manage.py runserver --nothreading and try again. Now you should be able to visualize the full synchronous behavior.

Related