Hi,
I want to kill / stop a thread at an event, within my websocket class, how can this be done? Will the same thread, that was opened when the connection was made, also be killed?
I want to kill / stop a thread at an event, within my websocket class, how can this be done? Will the same thread, that was opened when the connection was made, also be killed?
Python:
import threading
from threading import Thread
import signal, sys, json
import os
import shutil
import logging
import time
import sqlite3
from sqlite3 import Error
from functools import partial
from SimpleWebSocketServer import WebSocket, SimpleWebSocketServer
logger = logging.getLogger(__name__)
wss = []
PORTNUM = 8001
class Echo(WebSocket):
def __init__(self, state):
threading.Thread.__init__(self)
def controll(self, state):
# Loop
while True:
self.do_something(state)
time.sleep(5)
def do_something(self, state)
for ws in wss:
ws.sendMessage(sendMessage(calculatation_something(state) #Broadcast of messane
def handleMessage(self):
if self.data is None:
self.data = ''
data = (self.data)
func(data)
print("Echoing check received data: '%s'" % state)
def handleConnected(self):
#stop_threads = False
print(self.address, 'connected')
if self not in wss:
wss.append(self)
t2 = threading.Thread(target=self.controll, args=('on'))
t2.start()
for thread in threading.enumerate():
print(thread.name)
print("Connected")
def handleClose(self):
#stop_threads = True
#self.t2.kill()
#self.t2.join()
#print('thread killed')
wss.remove(self)
print(self.address, 'closed')
print("Disconnected")
def main():
print("Websocket server on port %s" % PORTNUM)
server = SimpleWebSocketServer('', PORTNUM, Echo)
try:
server.serveforever()
finally:
server.close()
if __name__ == "__main__":
main()