在接受连接之前直接关闭连接时,Uvicorn未关闭连接

时间:2019-05-21 15:47:37

标签: django django-channels daphne uvicorn

我正在将Django通道与uvicorn一起使用,并且具有以下类型的代码:

$ heroku features:disable runtime-heroku-exec

基本上,如果经过中间件后,如果不使用,我将关闭连接。当我与Daphne一起运行时,它的运行情况非常好。否则,当我通过uvicorn服务器运行它时,它将引发以下错误:

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()

但是,当我在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.accept()时,它不会引发任何错误。谁能帮我这个忙。

提前谢谢!

1 个答案:

答案 0 :(得分:0)

这显然是issue in channels,在套接字设置期间尚未准备好调用close()方法。

据我所读,问题的解决方案正是您在做什么,即先接受连接,然后关闭它。