02/02/2019 Django 3.0 has been released. Django 1.0 was released in 2008 and 2.0 was released in December 2017. And 3.0 was released on December 2nd of this year. It took about 10 years to upgrade from 1 to 2, but it was a surprising speed of less than 2 years from 2 to 3. What on earth have I been doing in the last two years compared to that? Now let's take a look at the new features that I personally care about in Django 3.0!
I was curious about this. That said, I've never touched ASGI and officially supported it! Even though I was told, it was like that. But I have WSGI-san ... However, when I looked it up, it seemed that ASGI seemed to be something that couldn't be ignored.
According to the Release Notes, asynchronous functionality is currently not in WSGI and is only supported when run in ASGI. (But do you think we will support it in the future?). According to ASGI Official, "ASGI is WSGI's spiritual successor (spiritual sequel. wiki /% E7% B2% BE% E7% A5% 9E% E7% 9A% 84% E7% B6% 9A% E7% B7% A8)) ”. In this way, WSGI, which seemed to be my strong ally, seems to be backward compatible with ASGI.
It doesn't matter, but the word "spiritual sequel" is cool. It seems that Xenosaga for Xenogears, Perfect Dark for GoldenEye 007, etc. correspond to it. However, WSGI and ASGI are not games or fiction works, and are sequels really suitable? I thought, so I will read it as a spiritual successor. I wrote that in the title.
If so, the story is quick, so I tried running Django with ASGI. Create an environment with Docker.
sudo docker run --rm -it -p 8000:8000 python:3.8 bash
I use python3.8. By the way, Django 3.0 supports python 3.6, 3.7, and 3.8.
pip install django daphne
Install daphne
along with django
. daphne
looks like a server for ʻASGI. ʻU WSGI
in WSGI
is probably daphne
in ʻASGI`. surely.
By the way, daphne seems to be called daphne. django is django, but daphne's d should be pronounced properly.
django-admin startproject app
cd app
daphne -b 0.0.0.0 -p 8000 app.asgi:application
After installing, create a project and try running it immediately.
It worked, but I don't know what makes me happy. Well, of course. Even though the asynchronous function is for sale, it only communicates synchronously.
was. https://channels.readthedocs.io/en/latest/tutorial/index.html It seems that Django channels can use web sockets with Djang, and there is a polite chat app tutorial. It is my soul to try running a chat application made by copying this with daphne. However, I'm not sure about the relationship between Django channels and this ASGI support. Well, but don't go too deep as you may find out as you use it.
I will put the finished product made according to the tutorial here here.
I will try to move it. It is in the state of command: python manage.py runserver 0.0.0.0:8000
.
sudo docker-compose up -d
I confirmed the operation with 2 windows, but you can use chat properly.
Now, let's move this with daphne
. Rewrite command
as follows:
services:
app:
build: .
- command: python manage.py runserver 0.0.0.0:8000
+ command: daphne -b 0.0.0.0 -p 8000 mysite.asgi:application
volumes:
- .:/srv
ports:
This should work as I expected ...
sudo docker-compose down
sudo docker-compose up -d
Hmm, it doesn't work.
When I read the error message, it says that websocket cannot be used.
172.17.0.1:52480 - - [06/Dec/2019:07:36:28] "GET /chat/a/" 200 1413
172.17.0.1:52488 - - [06/Dec/2019:07:36:28] "WSCONNECTING /ws/chat/a/" - -
2019-12-06 07:36:29,471 ERROR Exception inside application: Django can only handle ASGI/HTTP connections, not websocket.
File "/usr/local/lib/python3.8/site-packages/daphne/cli.py", line 30, in asgi
await self.app(scope, receive, send)
File "/usr/local/lib/python3.8/site-packages/django/core/handlers/asgi.py", line 144, in __call__
raise ValueError(
Django can only handle ASGI/HTTP connections, not websocket.
172.17.0.1:52488 - - [06/Dec/2019:07:36:29] "WSDISCONNECT /ws/chat/a/" - -
Let's take a look around line 144 of asgi.py.
async def __call__(self, scope, receive, send):
"""
Async entrypoint - parses the request and hands off to get_response.
"""
# Serve only HTTP connections.
# FIXME: Allow to override this.
if scope['type'] != 'http':
raise ValueError(
'Django can only handle ASGI/HTTP connections, not %s.'
% scope['type']
)
It seems that it throws an exception when it is not HTTP connections. What the hell is this?
daphne app.asgi: application
using Django 3.0 is wrong in the first placeI think it's either, but I'm not sure. The mystery is only deepening. However, it is officially stated that ASGI can support Django asynchronously, so I will continue to pay close attention to it.
Goodbye WSGI. I have ASGI ...
Recommended Posts