python - Pythonic way to know when / why my threads exited -


context: have script, runs indefinitely, monitors simple queue of urls need downloaded. if url enters queue, script checks if spawned thread url , if hasn't, spawns thread who's job fetch data url periodically until url returns 404 (which know happen because urls available specified period of time) @ point, call sys.exit raise systemexit exception , mark termination understand it.

question: able log specific time when thread exits, if exits other reason besides call sys.exit , gather meta data why exited possible. best way this? threads pass info parent spawned them when exit?

code:

a simplified example of code

    class mythread(threading.thread):         def __init__(self, sf, id):             threading.thread.__init__(self)             self.sourcefile = [sf]             self.id = id           def run(self):              #do stuff until encounter 404, @ point, i'll call sys.exit  if __name__ == '__main__':     while true:          #logic check queue, if there new url, spawn new thread         #for each new thread in queue:              t = mythread(file, i)             t.start()             threads.append(t)  

do this:

import datetime  class mythread(threading.thread)     termination_cause = none     termination_time = none      #snip      def run(self):         try:             # stuff         except exception e:  # wouldn't recommend this, asked             self.termination_cause = e  # if exception occurred, here         finally:             self.termination_time = datetime.datetime.now() 

as exit try block, either because exception raised or because block ended, finally block execute, , termination_time attribute set.


note wouldn't consider practice raise systemexit close thread. why don't block flow end?

def run(self):     try:         while 1:             if url_returns_404(url):                 break             # thing url     finally:         self.termination_time = datetime.datetime.now() 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -