2

So I have some threads in an application that monitor a temporarily available file on some 3rd party endpoint. I want them to perform their purpose in life for no more than a day, then terminate. What's the best way to do that?

There are lots of questions on S/O about making a thread terminate if it times out on some function (like a TCP hangs connection or something) but my goal centers more around giving the thread a lifespan independent of the functions it performs. I whipped up a quick demo of what i'm thinking so far:

class Task(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.life = datetime.utcnow() + timedelta(seconds=60)

    def run(self):
        while True:
                if datetime.utcnow() < self.life:
                        #do stuff that i do 
                        print "hi"
                        time.sleep(5)
                else:
                        break

t = Task()
t.start()

This works fine but am I thinking about this in a dumb way? Is there a better way to handle it?

Brad
  • 6,106
  • 4
  • 31
  • 43
  • Note that if #do stuff that i do either takes a long time computationonally and/or blocks for a long time, you could exceed your selfimposed window (and at a minimum, on average I would expect your loop as written to exceed the 60 seconds by about 2.5 seconds) – Foon Sep 11 '13 at 16:39
  • The thing is, what if instead of `print 'hi'; time.sleep(5)` you have a some code that takes a long time to run? It won't stop until it is finished. – Paco Sep 11 '13 at 16:40
  • @foon sure. self.life would actually be 24hrs from the time the thread was spawned. The code inside takes about 1-2 seconds max to execute. I've monitored it to be sure. Also, the moment in time at which the thread dies isn't very important. essentially, it will only do meaningful work for about the first 4 hrs of its life. The lifespan is in place to make sure that it doesn't run too long. – Brad Sep 11 '13 at 16:44
  • @paco - i don't understand what you mean by "It won't stop until it is finished" – Brad Sep 11 '13 at 16:45
  • Relevant: http://stackoverflow.com/a/325528/132382 – pilcrow Sep 11 '13 at 16:46
  • @pilcrow ? why is that relevant? I'm not forcibly killing the thread, i'm just breaking out of the loop and the thread will finish and exit. – Brad Sep 11 '13 at 16:50
  • It's relevant because the link essentially refines and endorses your approach, and compares it to the more laborious and riskier asynchronous termination you appeared to be asking about. – pilcrow Sep 11 '13 at 17:41
  • @Brad: replace `print 'hi'; time.sleep(5)` with a function that takes about 2 hours, like another `time.sleep` call. How would you stop it ? If its running you have no escape way, it has to finish before you can break out – Paco Sep 12 '13 at 08:29

1 Answers1

2
def run(self):
    while datetime.utcnow() < self.life:
        #do stuff that i do 
        print "hi"
        time.sleep(5)
dugres
  • 12,613
  • 8
  • 46
  • 51