0

I'm having a tough time trying to develop a threaded app wherein the threads are each doing REST calls over the network.

I need to kill active threads after a certain timeout. I've tried every python 2.7.x approach I've seen on here and can't get this working.

I'm using python 2.7.6 on OEL linux (3.8.13-44.1.1.el6uek.x86_64).

Here is a simplified snippet of code:

class cthread(threading.Thread):
    def __init__(self, cfg, host):
        self.cfg  = cfg
        self.host = host
        self.runf = False
        self.stop = threading.Event()
        threading.Thread.__init__(self. target=self.collect)

    def terminate(self):
        self.stop.set()

    def collect(self):
        try:
            self.runf = True
            while (not self.stop.wait(1)):
                # Here I do urllib2 GET request to a REST service which could hang
                <rest call>
         finally:
             self.runf = False

timer_t1 = 0

newthr = cthread(cfg, host)
newthr.start()

while True:
    if timer_t1 > 600:
        newthr.terminate()
        break
    time.sleep(30)
    timer_t1 += 30

Basically after my timeout period I need to kill all remaining threads, either gracefully or not.

Haven't a heck of a time getting this to work.

Am I going about this the correct way?

BenH
  • 690
  • 1
  • 11
  • 23
  • http://stackoverflow.com/questions/13821156/timeout-function-using-threading-in-python-does-not-work – Frodon Aug 18 '16 at 15:08

1 Answers1

0

There's no official API to kill a thread in python.

In your code relying on urllib2 you might periodically pass timeout left for your threads to run from the main loop and use urllib2 with the timeout option. Or even track the timers in threads exploiting the same approach with urllib2.

Oleg Kuralenko
  • 11,003
  • 1
  • 30
  • 40
  • I had to move this to using subprocess instead of threads... There simply is no way to do this reliably with python threads, as least not with 2.7.x python. Even Perl is better at threads than python 2.7. Of course now I have to worry about memory footprint since I'm using subprocesses... Long term plan at this point is to move this to Java now. – BenH Oct 17 '16 at 12:22