Django is synchronous or asynchronous?

拈花ヽ惹草 提交于 2020-06-24 16:56:22

问题


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.


回答1:


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 do not use twisted or asyncio.

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. So probably most real Django views will fail, as one of the reasons of Django is to use an ORM for data base access.sections



来源:https://stackoverflow.com/questions/58548089/django-is-synchronous-or-asynchronous

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!