【发布时间】:2019-05-21 15:47:37
【问题描述】:
我正在使用带有 uvicorn 的 Django 频道,并且我有以下类型的代码:
async def connect(self):
"""Accept connect if user has been provided by middleware"""
self.user = self.scope.get('user')
if self.user:
await self.accept()
else:
await self.close()
基本上,如果在通过中间件之后如果没有使用,我将关闭连接。当我用 Daphne 运行它时,它工作得非常好。否则,当我通过 uvicorn 服务器运行它时,它会抛出以下错误:
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/home/coldbrewtech/frnd/backend/env/lib/python3.6/site-packages/uvicorn/protocols/websockets/websockets_impl.py", line 146, in run_asgi
result = await self.app(self.scope, self.asgi_receive, self.asgi_send)
File "/home/coldbrewtech/frnd/backend/env/lib/python3.6/site-packages/uvicorn/middleware/asgi2.py", line 7, in __call__
await instance(receive, send)
File "/home/coldbrewtech/frnd/backend/env/lib/python3.6/site-packages/channels/consumer.py", line 59, in __call__
[receive, self.channel_receive], self.dispatch
File "/home/coldbrewtech/frnd/backend/env/lib/python3.6/site-packages/channels/utils.py", line 59, in await_many_dispatch
await task
File "/home/coldbrewtech/frnd/backend/env/lib/python3.6/site-packages/channels/utils.py", line 51, in await_many_dispatch
result = task.result()
File "/home/coldbrewtech/frnd/backend/env/lib/python3.6/site-packages/uvicorn/protocols/websockets/websockets_impl.py", line 226, in asgi_receive
data = await self.recv()
File "/home/coldbrewtech/frnd/backend/env/lib/python3.6/site-packages/websockets/protocol.py", line 419, in recv
return_when=asyncio.FIRST_COMPLETED,
File "/usr/lib/python3.6/asyncio/tasks.py", line 311, in wait
fs = {ensure_future(f, loop=loop) for f in set(fs)}
File "/usr/lib/python3.6/asyncio/tasks.py", line 311, in <setcomp>
fs = {ensure_future(f, loop=loop) for f in set(fs)}
File "/usr/lib/python3.6/asyncio/tasks.py", line 526, in ensure_future
raise TypeError('An asyncio.Future, a coroutine or an awaitable is '
TypeError: An asyncio.Future, a coroutine or an awaitable is required
但是当我在await self.close() 之前添加await self.accept() 时,它不会抛出任何错误。谁能帮我解决这个问题。
提前致谢!!!
【问题讨论】:
-
Uvicorn 问题同样的错误:github.com/encode/uvicorn/issues/415
标签: django django-channels daphne uvicorn