This are some the code that I used for my udp server. My problem is when I call the method terminate, It will only stop the listening loop after it receives another message from udp socket. I want to stop the thread immediately when I call terminate method. Or is there a better way of doing this? Thanks
class StartThread:
def __init__(self):
self._running = True
self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.s.bind(("0.0.0.0", 1234))
def terminate(self):
self._running = False
def run(self):
while self._running:
data, addr = self.s.recvfrom(1024)
c = StartThread()
t = Thread(target=c.run)
t.start()
import select
orimport twisted
(needs to be installed) for alternatives to threads. – User