There is one thing I feel I don't understand though. The plugin is working, but I'm not comfortable that I can't simulate ONE expected behavior...
(Again, writing this at work, so using my simple markup version of this...)
This is my class:
- Code: Select all
class XYZ(object):
def __init__(self, dev, logger, sleep, stopThread):
self.dev = dev
self.logger = logger
self.sleep = sleep
self.stopThread = stopThread
def startSocket(self):
#Basically some stuff here that gets relevant socket data from the device's config, and starts it up.
def stopSocket(self):
#pretty much just socket.close()
def runDevConcurrentThread(self):
try:
while True:
self.sleep(sleeptime)
try:
# This try inside of the while loop is because if there's no data available at the socket, I still want to do stuff.
#read data from socket here
#parse and process
except:
# Do my other non-socket reading stuff here
except self.stopThread:
self.logger.debug("exception hit, process exiting")
pass
def stopDevConcurrentThread(self):
self.stopThread = True
...and the relevant parts of my code outside of the class that refer to it.
- Code: Select all
def deviceStartComm(self, dev):
#create an instance of the class with specs for this device
mydevice[dev.id] = XYZ(dev, self.logger, self.sleep, self.stopThread)
#start the socket, then start the thread
self.mydevice[dev.id].startSocket()
Thread(target=self.mydevice[dev.id].runDevConcurrentThread()).run()
def deviceStopComm(self,dev):
mydevice[dev.id].stopDevConcurrentThread()
mydevice[dev.id].stopSocket()
Now at this point, everything just works great. Plugin starts and stops without problems. Individual devices start and stop without problems, and live in their own worlds.
BUT, when I call the deviceStopComm, while the device DOES stop, and does so gracefully, it does NOT process the code after the exception (where I've included some comment above). There's no actual code there, but I want to see that debug line to make sure it's doing what I think it's doing. I can NOT seem to access that part of the code.
I suspect it has to do with the sleep method and stopThread, which I think you guys have defined somewhat specially to stop the thread in this manner. It works... but doesn't process the exception.
I've tried removing the stipulation from the exception (so, just "except:") and it still doesn't catch. But again, the thread seems to shut down without any issue.
The stopDevConcurrentThread does seem to correctly alter the state of self.stopThread (alternately, from the main class I can just set mydevice[dev.id].stopThread = False and that alters the state correctly too, so the stopDevConcurrentThread is probably entirely unnecessary).
Am I doing something wrong with passing the sleep and stopThread bits in when I instantiate the class?
Thanks! Super grateful for the help so far - I probably couldn't have got this running without you guys!