侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Django3.0 异步通信初体验(小结)

框架(架构)  /  管理员 发布于 7年前   152

此前博主曾经写过一篇博文,介绍了Django3.0的新特性,其中最主要的就是加入对ASGI的支持,实现全双工的异步通信。

2019年12月2日,Django终于正式发布了3.0版本。怀着无比的期待,我们来尝试一下吧!

(附ASGI官方文档地址:https://asgi.readthedocs.io/en/latest/extensions.html)

一、创建Django3工程

利用Pycharm的方便,直接通过virtualenv创建虚拟环境,并安装Django3.0。

打开控制台,看看都安装了哪些库:

(venv) D:\work\for_test\django3>pip listPackage  Version------asgiref  3.2.3Django   3.0pip    10.0.1pytz    2019.3setuptools 39.1.0sqlparse  0.3.0

除了pytz和sqlparse,又自动安装了asgiref。

asigref由Django软件基金会开发和维护,是一个Django生态中的库。

先启动一下服务器,看看Django是否正常运行:

没毛病,可以把服务器关了!

二、学习官方文档

毕竟是非常重要和复杂的新特性,先到官网取取经,看看文档怎么写的。

找了一圈,What?就这么点东西?

先康康吧。

How to deploy with ASGIAs well as WSGI, Django also supports deploying on ASGI, the emerging Python standard for asynchronous web servers and applications.Django's startproject management command sets up a default ASGI configuration for you, which you can tweak as needed for your project, and direct any ASGI-compliant application server to use.Django includes getting-started documentation for the following ASGI servers:How to use Django with DaphneHow to use Django with UvicornThe application objectLike WSGI, ASGI has you supply an application callable which the application server uses to communicate with your code. It's commonly provided as an object named application in a Python module accessible to the server.The startproject command creates a file <project_name>/asgi.py that contains such an application callable.It's not used by the development server (runserver), but can be used by any ASGI server either in development or in production.ASGI servers usually take the path to the application callable as a string; for most Django projects, this will look like myproject.asgi:application.WarningWhile Django's default ASGI handler will run all your code in a synchronous thread, if you choose to run your own async handler you must be aware of async-safety.Do not call blocking synchronous functions or libraries in any async code. Django prevents you from doing this with the parts of Django that are not async-safe, but the same may not be true of third-party apps or Python libraries.Configuring the settings moduleWhen the ASGI server loads your application, Django needs to import the settings module ― that's where your entire application is defined.Django uses the DJANGO_SETTINGS_MODULE environment variable to locate the appropriate settings module. It must contain the dotted path to the settings module. You can use a different value for development and production; it all depends on how you organize your settings.If this variable isn't set, the default asgi.py sets it to mysite.settings, where mysite is the name of your project.Applying ASGI middlewareTo apply ASGI middleware, or to embed Django in another ASGI application, you can wrap Django's application object in the asgi.py file. For example:from some_asgi_library import AmazingMiddlewareapplication = AmazingMiddleware(application)

文档短小无力,内容稀少!

总结就以下几点:

  • 不能用python manage.py runserver的方式启动ASGI服务器,这只会启动传统的、默认的WSGI服务器,也就是老版本的东西
  • 要启动ASGI服务器,你需要使用Daphne或者Uvicorn。推荐使用Daphne,这是Django软件基金会开发的一个基于ASGI (HTTP/WebSocket)的服务器。
  • 有一个myproject.asgi:application文件,是ASGI通信的接口,Django默认自带
  • 你可以配置DJANGO_SETTINGS_MODULE 环境,或者使用默认的your_project.settings
  • 可以使用第三方ASGI中间件

再简单粗暴点,全文的意思是,你需要安装Daphne,然后调用Django的application来启动ASGI服务器。

好吧,我们看看Daphne,点击Django给的连接,跳转到相关页面,内容更少:

How to use Django with DaphneDaphne is a pure-Python ASGI server for UNIX, maintained by members of the Django project. It acts as the reference server for ASGI.Installing DaphneYou can install Daphne with pip:python -m pip install daphneRunning Django in DaphneWhen Daphne is installed, a daphne command is available which starts the Daphne server process. At its simplest, Daphne needs to be called with the location of a module containing an ASGI application object, followed by what the application is called (separated by a colon).For a typical Django project, invoking Daphne would look like:daphne myproject.asgi:applicationThis will start one process listening on 127.0.0.1:8000. It requires that your project be on the Python path; to ensure that run this command from the same directory as your manage.py file.

翻译过来就是:

  • pip安装daphne
  • 执行daphne myproject.asgi:application命令,启动ASGI服务器
  • 浏览器访问127.0.0.1:8000

咱们照做!

三、启动ASGI服务器

通过pip install daphne即可安装最新版本的daphne:

Collecting pycparser (from cffi!=1.11.3,>=1.8->cryptography>=2.7->autobahn>=0.18->daphne)Installing collected packages: idna, hyperlink, zope.interface, attrs, six, Automat, constantly, PyHamcrest, incremental, pycparser, cffi, cryptography, pyopenssl, pyasn1, pyasn1-modules, service-identity, twisted, txaio, autobahn, daphneSuccessfully installed Automat-0.8.0 PyHamcrest-1.9.0 attrs-19.3.0 autobahn-19.11.1 cffi-1.13.2 constantly-15.1.0 cryptography-2.8 daphne-2.4.0 hyperlink-19.0.0 idna-2.8 incremental-17.5.0 pyasn1-0.4.8 pyasn1-modules-0.2.7 pycparser-2.19 pyopenssl-19.1.0 service-identity-18.1.0 six-1.13.0 twisted-19.10.0 txaio-18.8.1 zope.interface-4.7.1

为了安装daphne,需要额外安装这么多依赖包!我们再pip list看一下:

(venv) D:\work\for_test\django3>pip listPackage     Version---------------- -------asgiref     3.2.3attrs      19.3.0autobahn     19.11.1Automat     0.8.0cffi       1.13.2constantly    15.1.0cryptography   2.8daphne      2.4.0Django      3.0hyperlink    19.0.0idna       2.8incremental   17.5.0pip       10.0.1pyasn1      0.4.8pyasn1-modules  0.2.7pycparser    2.19PyHamcrest    1.9.0pyOpenSSL    19.1.0pytz       2019.3service-identity 18.1.0setuptools    39.1.0six       1.13.0sqlparse     0.3.0Twisted     19.10.0txaio      18.8.1zope.interface  4.7.1

不管了,就当没看见。

安装成功后,我们会获得一个daphne可执行命令,下面我们来启动服务器吧。

执行daphne django3.asgi:application命令。(将其中的django3换成你的工程名字,在manage.py文件所在的路径下执行)

(venv) D:\work\for_test\django3>daphne django3.asgi:application2019-12-04 10:20:07,637 INFO   Starting server at tcp:port=8000:interface=127.0.0.12019-12-04 10:20:07,637 INFO   HTTP/2 support not enabled (install the http2 and tls Twisted extras)2019-12-04 10:20:07,637 INFO   Configuring endpoint tcp:port=8000:interface=127.0.0.12019-12-04 10:20:07,637 INFO   Listening on TCP address 127.0.0.1:8000

当然,我们也是可以指定ip和port等参数的,详细请学习daphne文档https://pypi.org/project/daphne/

读一下人家的启动信息:

  1. 默认启动地址是127.0.0.1:8000
  2. 没有开启HTTP/2的支持(需要安装额外的包)
  3. 配置了端点,开始监听端口

Nice,通过浏览器来访问一下吧!

依然是熟悉的白底火箭图!图片我就不贴了,看起来没问题。

可是,这是基于HTTP的同步通信,还不是异步请求!让我们尝试一下websocket吧!

四、尝试Websocket

浏览器中按F12进入‘坦克模式',再进入console控制台,尝试发送一个Websocket请求:

忽视前面的css问题,重点在于这个ws请求,居然无法建立连接!返回状态码500,也就是服务器错误!

再看看Pycharm后台的信息:

127.0.0.1:5991 - - [04/Dec/2019:10:30:05] "WSCONNECTING /" - -2019-12-04 10:30:05,246 ERROR  Exception inside application: Django can only handle ASGI/HTTP connections, not websocket. File "d:\work\for_test\django3\venv\lib\site-packages\daphne\cli.py", line 30, in asgi  await self.app(scope, receive, send) File "d:\work\for_test\django3\venv\lib\site-packages\django\core\handlers\asgi.py", line 146, in __call__  % scope['type'] Django can only handle ASGI/HTTP connections, not websocket.127.0.0.1:5991 - - [04/Dec/2019:10:30:05] "WSDISCONNECT /" - -

重点在这句Django can only handle ASGI/HTTP connections, not websocket.

什么?Django不支持Websocket?说好的ASGI,说好的全双工异步通信呢?

难道是我哪里搞错了?不行,我得看看源码去!

五、Django3.0源码

一路点点点,翻了个遍,发现Django3.0与之前的2.2关于ASGI的区别就是多了下面两个文件:

django.core.asgi很简单,一看就懂,不多说:

import djangofrom django.core.handlers.asgi import ASGIHandlerdef get_asgi_application():  django.setup(set_prefix=False)  return ASGIHandler()

关键是django.core.handlers.asgi这个文件,不到300行,总共就2个类,代码就不贴了:

  • ASGIRequest:继承了HttpRequest类,一看就知道是构造请求对象
  • ASGIHandler:继承了base.BaseHandler,这个就类似WSGIHandler,用于具体处理ASGI请求

让我们看看它的其中一段代码:

# 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']  )

这就是我们前面在浏览器中发送ws请求,但是无法建立连接的原因!

如果scope的type属性不是http,那么弹出ValueError异常,并提示不支持该类连接!

再看看人家写的注释,明明白白的写着Serve only HTTP connections. FIXME: Allow to override this.。翻译过来就是只支持HTTP连接,请自己重写这部分内容修复这个缺陷。FIXME!FIXME!

好吧,我承认白高兴了一场。

六、总结

与Django3.0关于异步通信的初体验很不好,有下面几点猜测:

  • 可能我水平不行,不会用Django
  • 可能不是通过Websocket,而是别的手段与ASGI通信
  • Django真的目前只提供了个接口,内部实现还没做

由于相关资料太少,多方查找,据说:

  • Django会在后续的版本陆续实现完整的原生的异步通信能力,从同步机制切换到可兼容的异步机制
  • 目前3.0阶段,要与websocket通信依然得通过django-channel库,还不是原生支持

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


  • 上一条:
    在django中自定义字段Field详解
    下一条:
    Django Form and ModelForm的区别与使用
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Filament v3.1版本发布(0个评论)
    • docker + gitea搭建一个git服务器流程步骤(0个评论)
    • websocket的三种架构方式使用优缺点浅析(0个评论)
    • ubuntu20.4系统中宿主机安装nginx服务,docker容器中安装php8.2实现运行laravel10框架网站(0个评论)
    • phpstudy_pro(小皮面板)中安装最新php8.2.9版本流程步骤(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2018-05
    • 2020-02
    • 2020-03
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-11
    • 2021-03
    • 2021-09
    • 2021-10
    • 2021-11
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-08
    • 2023-08
    • 2023-10
    • 2023-12
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客