gpt4 book ai didi

django - Uvicorn 在接受连接前直接关闭连接时不关闭连接

转载 作者:行者123 更新时间:2023-12-05 07:23:05 28 4
gpt4 key购买 nike

我将 Django Channels 与 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()

基本上,如果在使用中间件后没有使用,我将关闭连接。当我用 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() 时,它不会抛出任何错误。谁能帮我解决这个问题。

提前致谢!!!

最佳答案

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

从我在问题中可以读到的内容来看,解决方案正是您正在做的,即先接受连接然后关闭它。

关于django - Uvicorn 在接受连接前直接关闭连接时不关闭连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56241977/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com