Isotropix Forums

threading socket make crash

Clarisse Scripting related topics

threading socket make crash

Unread postby qinyao » Tue May 05, 2020 11:45 am

I am using threading to make a socket client in clarisse,
every time when I close the GuiWindow, I found the thread not be stoped, when I run my script again, always crash.

Anyone can tell me how to join the threads when I close the GuiWindow?


Code: Select all
# Clarisse socket client
import ix
app = ix.application

import socket,threading,time

class ML_BridgeClient():

    def __init__(self):
        #super(ML_BridgeClient, self).__init__()

        try:
            self.startThread()
        except:
            pass


    def startClient(self):

        HOST = '127.0.0.1' 
        PORT = 37373

        try:
            client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            client.connect((HOST, PORT))

            connect_label.set_label("Connected!")
            window.redraw()
           
            client.sendall(b'Clarisse')

            while True:
                data = client.recv(2048)
                if data:
                    print('Received', repr(data))

            client.close()

        except:
            connect_label.set_label("Missing connection, trying again...")
            window.redraw()
            time.sleep(2)
            self.startClient()


    def startThread(self):
        self.t = threading.Thread(target=self.startClient)
        self.t.start()



#<<<<<<<<<<<<<<<<<<<<<<<<<<< UI

version = "1.0"

app_x = ix.application.get_event_window().get_position()[0]
app_y = ix.application.get_event_window().get_position()[1]
app_w = ix.application.get_event_window().get_width()
app_h = ix.application.get_event_window().get_height()

window_x = 350
window_y = 80

clarisse_win = ix.application.get_event_window()

window = ix.api.GuiWindow(clarisse_win, app_x+(app_w-window_x)/2, app_y+(app_h-window_y)/2, window_x, window_y,"Mad Library Bridge v"+version )

window.set_resizable (False)

panel = ix.api.GuiPanel(window,0,0,window_x,window_y)

connect_label =  ix.api.GuiLabel(panel,20,20,360,40,"Trying to connect...")


print("Tring to connect to server...")
bridge = ML_BridgeClient()

window.show()

while window.is_shown(): ix.application.check_for_events()


qinyao
 
Posts: 85
Joined: Fri Aug 26, 2016 11:29 am

Re: threading socket make crash

Unread postby anemoff » Tue May 05, 2020 5:41 pm

Hi!

Your thread will not stop automatically after you close the window, you must do it yourself.
Here is a simplified example with a fix: when the window is closed, stop the infinite loop and join the thread.

python code

# Clarisse socket client
import ix
app = ix.application

import threading
import time

class ML_BridgeClient():

def __init__(self):
self.running = False
try:
self.startThread()
except Exception as e:
print(e)

def startClient(self):
i = 0
while self.running:
i += 1
connect_label.set_label("Working: iteration {}".format(i))
window.redraw()
time.sleep(1) # process data from the socket for example...

def startThread(self):
self.running = True
self.t = threading.Thread(target=self.startClient)
self.t.start()

def stop(self):
self.running = False
self.t.join()
print("Thread stopped")

#<<<<<<<<<<<<<<<<<<<<<<<<<<< UI

version = "1.0"

app_x = ix.application.get_event_window().get_position()[0]
app_y = ix.application.get_event_window().get_position()[1]
app_w = ix.application.get_event_window().get_width()
app_h = ix.application.get_event_window().get_height()

window_x = 350
window_y = 80

clarisse_win = ix.application.get_event_window()

window = ix.api.GuiWindow(clarisse_win, app_x+(app_w-window_x)/2, app_y+(app_h-window_y)/2, window_x, window_y,"Mad Library Bridge v"+version )
window.set_resizable (False)
panel = ix.api.GuiPanel(window,0,0,window_x,window_y)
connect_label = ix.api.GuiLabel(panel,20,20,360,40,"Trying to connect...")

bridge = ML_BridgeClient()

window.show()
while window.is_shown():
ix.application.check_for_events()

# the window is now closed, stop the working loop and join the thread
bridge.stop()


Cheers,
Anthony Nemoff
Isotropix
R&D Engineer
User avatar
anemoff
 
Posts: 367
Joined: Wed Jan 13, 2016 10:10 am


Return to Scripting