[Fixed]-Took too long to shut down and was killed

1πŸ‘

i got same problem when i try to disconnect the client from client side (by using Postman application ) , and i found the following solution , by raise a StopConsumer() Exception inside websoket disconnect method, kindly have a look at the following official DOCs link https://channels.readthedocs.io/en/stable/topics/consumers.html#closing-consumers

and here is a simple example :

from channels.consumer import SyncConsumer
from channels.exceptions import StopConsumer


class MySyncConsumer(SyncConsumer):

    def websocket_connect(self, event):
        print('websocket connected1...', event)

        self.send({
            'type': 'websocket.accept'
        })
        print('websocket connected2...', event)

    def websocket_receive(self, event):
        print('websocket recived message...', event)

    def websocket_disconnect(self, event):
        print('websocket disconnected...', event)  
        raise StopConsumer()

i don’t know if my case same your case but i hope this helpful .

πŸ‘€K.A

0πŸ‘

In case anyone is using syncWebsocketConsumer the syntax is the same as @K.A.’s answer, and resolved my issue as well. Thanks @K.A. for what should be the accepted answer.

Here is the async version of what fixed my issue.

    async def disconnect(self, close_code):
        print(close_code)
        await self.channel_layer.group_discard(
            self.room_group_name,
            self.channel_name
        )
        raise StopConsumer()
πŸ‘€MattG

Leave a comment