Django is synchronous or asynchronous?
Asked Answered
A

1

11

Django is synchronous or asynchronous?
I want to know that the Django Framework is Synchronous or Asynchronous. I have heard about the interview problems they ask about the framework you are using is synchronous or asynchronous. So I want to know the meaning of Synchronous and Asynchronous in terms of web development.

Allegorical answered 24/10, 2019 at 19:36 Comment(1)
Django is synchronous.Twenty
U
30

Django itself is synchronous.

each HTTP request will be handled completely synchronously.

However you have extensions like django-channels ( https://github.com/django/channels ) , which are asynchronous and are intended for web sockets / etc.

This is a little oversimplified: but synchronous programming is if you write code, that handles one HTTP request from beginning to end and that is executed in a thread or in a process and if one process / one thread handles only one request at a time.

With python in particular with asyncio or with twisted one can write code such, that one process/thread can handle multiple requests. Whenever one request waits for new data on the network to be received or for a chunk of data to be sent out it can handle another request until this other requests waits for network to be ready.

Django versions < 3.0 however do not use twisted or asyncio, thus http-requests are handled only in a synchronous manner.

New web servers / web apps however don't only handle http requests, but can also use web sockets. The Django channels module is built for handling web sockets. It is implemented with asyncio, which allows to handle many web sockets with one process only. it will interact with the synchronous parts of Django via messages (e.g. redis)

Addendum: as @Sayse pointed out Django 3.0 will support asynchronous code. However: ORM operations will still be synchronous only if I understand. They will fail in an async event loop with a SynchronousOnlyOperation exception (or they had to be offloaded to a thread pool). So probably most real Django views will fail or will depend on thread pools, as one of the reasons of Django is to use an ORM for data base access.

Updraft answered 24/10, 2019 at 19:48 Comment(7)
I added some more comments at the end of my anser. All text from the Words This is a little oversimplified is new.Updraft
django 3.0 will support asgiFavus
@Favus ORM operations will still be synchronous only if I understand. they will fail in an async event loop with a SynchronousOnlyOperation exception. so probably most requests as one of the reasons of django is to use ORM will still be synchronous or call threads for the ORM sections.Updraft
I'm confused so far as to what "synchronous" means in django. Does it only mean other requests have to wait till the current one finishes? In other, words within the processing of a single request I should be able to spawn threads, and processes like normal and handle their results with asyncio correct?Oswin
django < 3.0 is wsgi based. meaning, that one thread/process can only handle one request at a time. Though you could spawn threads or use asyncio after creating an asyncio loop I'd strongly advise against this practice (Depending on the application server you don't even know how long the parent thread / parent process will live. It could be terminated at the and of a request) It is also rather unconventional. You might consider celery or django channels or similar modules. Though they might be overkill I think the code would be less surprising and (in my opinion) less hackish.Updraft
If you decide to use uwsgi as application server, and it's fine for you, that the code runs on no other application server, then you could look at the uWSGI documentation ( uwsgi-docs.readthedocs.io/en/latest ) and search for things like 'uWSGI Spooler', 'uWSGI Mules' 'UWSGI Signals'Updraft
@JamieMarshall Are my answers (my recent comments) helpful? Please note as well, that I did a very minor rephrasing of my answer (insisting, that django < 3.0 does not use asyncio / twisted and mentioning, that ORM access can be offloaded to a thread pool)Updraft

© 2022 - 2024 — McMap. All rights reserved.