我按照本教程尝试将Channels
添加到我的应用中:
https://blog.heroku.com/in_deep_with_django_channels_the_future_of_real_time_apps_in_django
Django应用程序编译,但是当我尝试运行它时:
(virtualenv) root@computer:path# python manage.py runserver 0.0.0.0:8000
它给出错误:
File "/root/.virtualenvs/codebench/local/lib/python2.7/site-packages/channels/routing.py", line 75, in resolve_routing
raise ImproperlyConfigured("Cannot import channel routing %r: %s" % (routing, e))
ImproperlyConfigured: Cannot import channel routing 'codebench.routing.channel_routing': cannot import name ProtocolTypeRouter
根据这个 https://github.com/VincentTide/django-channels-celery-example/issues/1 它可能与Python版本有关,所以我做了:
(virtualenv) root@computer:path# alias python=python3
(virtualenv) root@computer:path# python -V
Python 3.5.2
(virtualenv) root@computer:path# python manage.py runserver 0.0.0.0:8000
Traceback (most recent call last):
File "manage.py", line 3, in <module>
import eventlet
ImportError: No module named 'eventlet'
(virtualenv) root@computer:path# pip install eventlet
Requirement already satisfied: eventlet in /root/.virtualenvs/codebench/lib/python2.7/site-packages
Requirement already satisfied: greenlet>=0.3 in /root/.virtualenvs/codebench/lib/python2.7/site-packages (from eventlet)
Requirement already satisfied: enum-compat in /root/.virtualenvs/codebench/lib/python2.7/site-packages (from eventlet)
Requirement already satisfied: enum34 in /root/.virtualenvs/codebench/lib/python2.7/site-packages (from enum-compat->eventlet)
现在我坚持:ImportError: No module named 'eventlet'
我安装了以下软件包:
channels==1.1.6
daphne==1.4.0
Django==1.9
eventlet==0.20.0
如果我遵循本教程:http://channels.readthedocs.io/en/1.x/getting-started.html
System check identified 1 issue (0 silenced).
February 16, 2018 - 23:19:32
Django version 1.9, using settings 'codebench.settings'
Starting Channels development server at http://0.0.0.0:8000/
Channel layer default (asgiref.inmemory.ChannelLayer)
Quit the server with CONTROL-C.
2018-02-16 23:19:32,935 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
2018-02-16 23:19:32,935 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
2018-02-16 23:19:32,935 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
2018-02-16 23:19:32,936 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
Traceback (most recent call last):
File "/root/.virtualenvs/codebench/local/lib/python2.7/site-packages/eventlet/hubs/hub.py", line 457, in fire_timers
timer()
File "/root/.virtualenvs/codebench/local/lib/python2.7/site-packages/eventlet/hubs/timer.py", line 58, in __call__
cb(*args, **kw)
File "/root/.virtualenvs/codebench/local/lib/python2.7/site-packages/eventlet/semaphore.py", line 145, in _do_acquire
waiter.switch()
File "/root/.virtualenvs/codebench/local/lib/python2.7/site-packages/eventlet/green/thread.py", line 41, in __thread_body
func(*args, **kwargs)
File "/root/.virtualenvs/codebench/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/root/.virtualenvs/codebench/local/lib/python2.7/site-packages/channels/management/commands/runserver.py", line 99, in inner_run
websocket_handshake_timeout=self.websocket_handshake_timeout,
File "/root/.virtualenvs/codebench/local/lib/python2.7/site-packages/daphne/server.py", line 101, in run
websocket_handshake_timeout=self.websocket_handshake_timeout
TypeError: __init__() got an unexpected keyword argument 'proxy_forwarded_proto_header'
如果我升级所有内容,除了Python 2.7.12:
asgi-redis==1.4.3
asgiref==2.1.5
channels==2.0.2
daphne==2.0.3
Django==1.11.10
runserver
会给出:
File "/root/.virtualenvs/codebench/local/lib/python2.7/site-packages/channels/apps.py", line 6, in <module>
import daphne.server # noqa
File "/root/.virtualenvs/codebench/local/lib/python2.7/site-packages/daphne/server.py", line 192
async def handle_reply(self, protocol, message):
^
SyntaxError: invalid syntax
如果我尝试在Python 3.5.2下重新安装所有必需的软件包:
需要twisted
:
apt install python3.6-dev
https://askubuntu.com/questions/966939/failed-building-wheel-for-twisted
现在我收到了随机错误:
2018-02-18 14:03:51,634 - ERROR - server - Exception inside application: 'NoneType' object has no attribute 'writeSequence'
File "/usr/lib/python3.5/asyncio/tasks.py", line 241, in _step
result = coro.throw(exc)
File "/root/.virtualenvs/python3/lib/python3.5/site-packages/channels/http.py", line 191, in __call__
await self.handle(body)
File "/root/.virtualenvs/python3/lib/python3.5/site-packages/asgiref/sync.py", line 108, in __call__
return await asyncio.wait_for(future, timeout=None)
File "/usr/lib/python3.5/asyncio/tasks.py", line 373, in wait_for
return (yield from fut)
File "/usr/lib/python3.5/asyncio/futures.py", line 361, in __iter__
yield self # This tells Task to wait for completion.
File "/usr/lib/python3.5/asyncio/tasks.py", line 296, in _wakeup
future.result()
File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result
raise self._exception
File "/usr/lib/python3.5/concurrent/futures/thread.py", line 55, in run
result = self.fn(*self.args, **self.kwargs)
File "/root/.virtualenvs/python3/lib/python3.5/site-packages/asgiref/sync.py", line 123, in thread_handler
return self.func(*args, **kwargs)
File "/root/.virtualenvs/python3/lib/python3.5/site-packages/channels/http.py", line 227, in handle
self.send(response_message)
File "/root/.virtualenvs/python3/lib/python3.5/site-packages/asgiref/sync.py", line 62, in __call__
return call_result.result()
File "/usr/lib/python3.5/concurrent/futures/_base.py", line 405, in result
return self.__get_result()
File "/usr/lib/python3.5/concurrent/futures/_base.py", line 357, in __get_result
raise self._exception
File "/root/.virtualenvs/python3/lib/python3.5/site-packages/asgiref/sync.py", line 76, in main_wrap
result = await self.awaitable(*args, **kwargs)
File "/root/.virtualenvs/python3/lib/python3.5/site-packages/daphne/server.py", line 196, in handle_reply
protocol.handle_reply(message)
File "/root/.virtualenvs/python3/lib/python3.5/site-packages/daphne/http_protocol.py", line 210, in handle_reply
http.Request.write(self, message.get("body", b""))
File "/root/.virtualenvs/python3/lib/python3.5/site-packages/twisted/web/http.py", line 1084, in write
self.channel.writeSequence(toChunk(data))
'NoneType' object has no attribute 'writeSequence'
此外:
(python3) root@computer:path# python manage.py shell
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import channels
>>> print(channels.__file__)
/root/.virtualenvs/python3/lib/python3.5/site-packages/channels/__init__.py
>>> from channels import Group
Traceback (most recent call last):
File "<console>", line 1, in <module>
ImportError: cannot import name 'Group'
答案 0 :(得分:0)
看起来好像你创建了一个Python 2 virtualenv。设置alias python=python3
将不起作用。您需要使用Python 3创建virtualenv,例如:
python3 -m venv newenv
然后在新的virtualenv中重新安装您的要求。
激活新环境后,python
将为python3
。您无需设置alias python=python3
。
答案 1 :(得分:0)
我最终得到以下版本:
asgi-redis==0.8.3
asgiref==0.9
autobahn==0.12.1
channels==0.9.3
daphne==0.9.1
dj-database-url==0.4.0
Django==1.9.3
haikunator==1.0.1
msgpack-python==0.4.7
psycopg2==2.7.4
redis==2.10.5
six==1.10.0
Twisted==15.5.0
txaio==2.2.1
zope.interface==4.1.3
原来Python版无关紧要,适用于2.7和3.5。 因为我正在安装https://github.com/jacobian/channels-example, 我还将数据库从MySQL更改为PostgreSQL。这个例子就像一个魅力。