Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Websocket threading - how to stop a thread? #108

Open
Jmmx1237 opened this issue Oct 31, 2022 · 2 comments
Open

Websocket threading - how to stop a thread? #108

Jmmx1237 opened this issue Oct 31, 2022 · 2 comments

Comments

@Jmmx1237
Copy link

Jmmx1237 commented Oct 31, 2022

Hi, is there a way, to kill a thread if a client is disconnect?

Please have a look to the handleClose section, I can't imagine how I can stop the same thread for a client, which the client has started.

import threading
import os
import logging
import time
from SimpleWebSocketServer import WebSocket, SimpleWebSocketServer


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):
         data = (self.data)
         func(data)
         print("Echoing check received data: '%s'" % state)

    def handleConnected(self):
        print(self.address, 'connected')

        # start a loop for each connected client
        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):
        #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()
@Jmmx1237
Copy link
Author

Jmmx1237 commented Nov 1, 2022

Ok, I got a solution:

import threading
from threading import Thread

from SimpleWebSocketServer import WebSocket, SimpleWebSocketServer
logger = logging.getLogger(__name__)

wss = []
PORTNUM = 8001

class Echo(WebSocket):

    def __init__(self, state):
        threading.Thread.__init__(self)
        self.thread_stop = False

    def controll(self, state):
        # Loop
        while not self.thread_stop:
            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):
        self.thread_stop = 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):
        self.thread_stop = True
        print('thread stop')
        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()`

@Jmmx1237
Copy link
Author

Jmmx1237 commented Nov 1, 2022

Is there a way, to do this in multiprocessing, and how can this implement?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant