安装:
python -m pip install -U channels -i https://pypi.tuna.tsinghua.edu.cn/simple/
指定源安装,否则会有很多依赖包下载的时候超时。
如果遇到Twisted安装错误,可以单独下载再用pip安装:https://pypi.org/simple/twisted/
运行的时候可能会出现以下错误:
File "/home/bekl/.local/lib/python3.5/site-packages/channels/management/commands/runserver.py", line 11, in <module>
from channels.routing import get_default_application
File "/home/bekl/.local/lib/python3.5/site-packages/channels/routing.py", line 10, in <module>
from channels.http import AsgiHandler
File "/home/bekl/.local/lib/python3.5/site-packages/channels/http.py", line 152, in <module>
class AsgiHandler(base.BaseHandler):
File "/home/bekl/.local/lib/python3.5/site-packages/channels/http.py", line 214, in AsgiHandler
@sync_to_async
File "/home/bekl/.local/lib/python3.5/site-packages/asgiref/sync.py", line 254, in __init__
self._is_coroutine = asyncio.coroutines._is_coroutine
AttributeError: module 'asyncio.coroutines' has no attribute '_is_coroutine'
根据提示,进入到sync.py的254行,再进入coroutines.py这个模块,可以看到它的确没有这个属性。
所以我们把'_is_coroutine'改为‘iscoroutine’即可:
使用方法详情查看手册:https://channels.readthedocs.io/en/latest/tutorial/part_1.html#