Do python cleanup properly on thread exit