I'm using django channels in order to send multiple responses to a single request. The code is something like this:
class terminal(WebsocketConsumer):
def connect(self):
self.accept()
def disconnect(self, close_code):
self.close()
def receive(self, text_data):
data_json = json.loads(text_data)
if data_json['event'] == 'initial':
self.t = threading.Thread(target=self.send_data)
self.t.daemon = True
self.t.start()
elif data_json['event'] == 'pause':
pass
print("done")
def send_data(self):
n = 100
end = 1000000
while (n + 1 < end)
# some code
self.send(json.dumps({'data':data})
n += 1
I've used thread to be able to listen to pause and other events while data is being sent to the client. The problem is that the thread keeps running after websocket gets disconnected.
Is there a way to kill a python thread in disconnect function? Or maybe a better way to implement this?