Is Django development server single-threaded?

Is Django development server really single-threaded? I mean: Can it render more that one request at the same time??

No it’s not.

Yes it can.

OK, could you explain why then first request takes 5sec; second-almost 10sec? It seems request2 is not being rendered until request1 is done



What operating system are you running this on, and what is your command line for running it?

There’s something wrong with your testing methodology.

Using the view:

def seven_seconds(request):
    print("Entering seven second delay")
    print("Leaving seven second delay")
    return HttpResponse("You waited seven seconds\n")

And this bash command:

for x in 1 2 3; do time curl http://localhost:8000/b/t1 & done; echo Starting

I get this output from the console:

You waited seven seconds

real    0m8.025s
user    0m0.010s
sys     0m0.000s
You waited seven seconds
You waited seven seconds

real    0m8.049s
user    0m0.001s
sys     0m0.011s

real    0m8.048s
user    0m0.011s
sys     0m0.000s

And this output from runserver:

(d39) tskww@Ubuntu2004:~/git/dt$ ./ runserver
Performing system checks...

System check identified no issues (0 silenced).
December 05, 2022 - 18:16:09
Django version 3.2.9, using settings 'dt.settings'
Starting development server at
Quit the server with CONTROL-C.
Entering seven second delay
Entering seven second delay
Entering seven second delay
Leaving seven second delay
Leaving seven second delay
Leaving seven second delay

If this thread is in any way related to your other thread regarding selenium, see the thread here at: Are calls to the SAME view synchronous?

I’m using Windows 11 + cmd (python runserver). I have reviewed this thread but I don’t really think browser is the reason because I send requests from Postman :confused:. My case is similar to that guy’s problem but unlike him I encouter same result even when request’s being sent to different views :confused:

receiving this behaviour sending requests at same time(±1sec)

There are still a couple of possibilities here. This may be a Windows-related issue.
It may be Daphne, or Daphne on Windows.

You haven’t provided any details about how you’re generating these requests. I’d suggest trying it directly using curl (or wget).

You might also want to try this with the standard runserver or runserver_plus (Werkzeug).

I think the concurrency here is also tricky because it’s through ASGI. If you’re running through ASGI and using time.sleep in view, is that not blocking? or does it not matter because all of the requests are in a magic async threadpool and python releases the gil on time.sleep? i’d expect asyncio.sleep to work with async concurrency. I haven’t done enough ASGI to understand the runtime thoroughly.

I’m not really good at low-level stuff like this and I’ve used time.sleep just to demonstrate problem. The real case is: API is serving to scrape webpage for data via Selenium and of course that scraping takes some time. I’ve noticed that if user1 sends request - selenium driver is collecting data, user2 must wait for user1 to receive response and only after that user’s2 request is being rendered, so as a result: user1 was waiting for response for ~10sec, user2 almost 20sec = it seems requests are being rendered serially

@massover @KenWhitesell problem is actually caused by daphne/asgi somehow, gonna find out why exactly, thank you for your help

It would be related to daphne/asgi on Windows - the Linux version does not exhibit any unusual behavior.

hmm :thinking:, I’ve tried it on docker (alpine) and faced the same behaviour, anyways I’ll switch to Ubuntu and try again there to figure it out completely

it’s definitely not OS problem, same result in my Ubuntu(requests rendered one by one), but when switched from daphne to default - requests are being rendered in parallel, so it’s something with daphne/my configuration but I was following channels tutorial ;(

problem solved by switching to Django 4 latest ver. from Django==3.2 :upside_down_face:

Just curious at this point, what version of Python are you using?

—using python 3.10.4