Brian
Brian

Reputation: 2305

Python threading with long running timers exiting unexpectedly

I am working on a python program which runs as a daemon and spawns several different long running threads with potentially separate sleep timers.

The issue I am running into is that the threads are dying after an unknown amount of time and I am not entirely sure why or how to diagnose the issues. I went and added (though not the final solution) a __del__ function to the class run as a thread to see what might be the issue, but am not sure what variables are available to determine what is causing the exit to occur.

I am not closer to determining the cause of the issue and am hoping to find some assistance.

A snippet of my main running program which is the top level daemon process is:

threads = []
sensorFolders = glob.glob(config._baseDir + '28*')
for folder in sensorFolders:
    sensorID = os.path.split(folder)[1]
    sensor = Sensor().getSensor(sensorID)
    threads.append(threading.Thread(target=sensor.startCheckin))
for thread in threads:
    thread.start()
for thread in threads:
    thread.join()

And the piece of the Sensor Class:

def startCheckin(self):
    while True:
        self.checkSensor()
        self.checkinSensor()
        self.postTemp()
        time.sleep(self._checkinInterval)

I can certainly add more code as needed but it is fairly basic in its implementation. I am just not sure what to try here as there does not appear (to a python noob) to be anything glaringly obvious as to what might be causing the abrupt closure of the threads.

Any help would be greatly appreciated!

Edit The issue appears to be that if the network drops for a moment, and the thread calls a url request, it does not know where to find the host and throws an exception. Unfortunately knowing this I am still unsure of how best to handle dealing with these exceptions.

Upvotes: 4

Views: 1454

Answers (1)

Luke Wahlmeier
Luke Wahlmeier

Reputation: 196

So I only see 3 possibilities here:

  1. The thread is throwing an exception, and you are not looking at or otherwise not noticing stderr
  2. Something being called by the thread is calling sys.exit, this will force only that thread to stop.
  3. If any blocking operations or locks are used its possible that the thread is deadlocking itself or blocking indefinitely on some io operation.

in any of these cases adding some thread dumping locking like this:

https://stackoverflow.com/a/2569696/3957645

should show you what is going on on that thread (or if its gone).

Upvotes: 1

Related Questions