django 通道需要很长时间才能关闭并在后端被杀死错误

问题描述 投票:0回答:1

我在这个项目中使用了django通道并在以下代码中创建了一个消费者:

class WSConsumer(WebsocketConsumer):

def connect(self):
    self.room_name = self.scope["url_route"]["kwargs"]["room_name"]
    self.stid=self.scope["url_route"]["kwargs"]["stid"]
    self.types=self.scope["url_route"]["kwargs"]["types"]

    self.room_group_name = f"station_{self.room_name}"

    if (self.types=="student"):
        try:
            workclass=WorkClass.objects.get(workclass_id=self.room_name)
            st=Station.objects.get(id=self.stid)
            if st in workclass.stations.all():
                st.state=True
                st.save()
                async_to_sync(self.channel_layer.group_add)(
                        self.room_group_name, self.channel_name
                )
                self.accept()
            else:
                self.disconnect(10)
        except WorkClass.DoesNotExist:
            self.disconnect(10)       
    elif(self.types=="teacher"):
        async_to_sync(self.channel_layer.group_add)(
            self.room_group_name, self.channel_name
        )
        self.accept()  
                   
        except WorkClass.DoesNotExist:
            raise StopConsumer()
        
    else:
        raise StopConsumer()
    

def disconnect(self, close_code):
  
    async_to_sync(self.channel_layer.group_discard)(
        self.room_group_name, self.channel_name
    )
    if(self.types=="student"):
        st=Station.objects.get(id=self.stid)
        st.state=False
        st.save()
        async_to_sync(self.channel_layer.group_send)(
            self.room_group_name,
            {"type":"list_stations","message":self.room_name,"roles":"stations"}
        )


async def websocket_disconnect(self, message):
    await self.channel_layer.group_send(
        self.room_group_name,
        {"type":"list_stations","message":self.room_name,"role":"stations"}
    )
    await super().websocket_disconnect(message)

def receive(self, text_data):
    text_data_json = json.loads(text_data)
    message = text_data_json["message"]
    role=text_data_json['roles']
    types=text_data_json['type']
    async_to_sync(self.channel_layer.group_send)(
        self.room_group_name,
        {"type":types,"message":message,"role":role}
    )
                  


# Receive message from room group
#list of method...

在设置中我使用了这个配置:

INSTALLED_APPS = [
'daphne',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

 #apps
]

ASGI_APPLICATION = "backend.asgi.application"

CHANNEL_LAYERS = {
"default": {
    "BACKEND": "channels_redis.core.RedisChannelLayer",
    "CONFIG": {
        "hosts": [("127.0.0.1", 6379)],
    },
},
}

问题是,在服务器终端一段时间后,它显示错误“花了很长时间才能关闭并被杀死”,这个时间是可变的。 同时我在前端通过另一台电脑上的IP地址访问来运行 无论我用谷歌搜索并尝试了所有解决方案,它都不起作用。

python django websocket django-channels
1个回答
0
投票

查看这里的代码,您有一个

WebsocketConsumer
,它正在同步运行,也就是说,尝试使用调用
async_to_sync
的断开连接函数。

当我们想让异步程序启动同步进程时,使用该函数。然而,我们的程序不是异步的。

这里我们需要一个

WebsocketConsumer
,而不是
AsyncWebsocketConsumer
对象。

请告诉我这可以解决您的问题!

© www.soinside.com 2019 - 2024. All rights reserved.