1

I know the "signal" module in python 2.7, in general, we can use the following code to timeout a main-thread function call:

import signal

def signal_handle_timeout:
   raise Exception("Timeout!")

signal.signal(signal.SIGALRM, signal_handle_timeout)
signal.alarm(seconds)

try:
   long_time_function()
except Exception as ex:
   print ex

But it only can run in main-thread, if try to run in sub-thread, it throw this error: 'ValueError: signal only works in main thread'

In Python, how can we timeout a function call in sub-thread?

user3003466
  • 323
  • 2
  • 5
  • 15
  • This question is answered at [How can you safeguard yourself from a flaky library call that might hang indefinitely?](http://stackoverflow.com/questions/36903966/how-can-you-safeguard-yourself-from-a-flaky-library-call-that-might-hang-indefin/36904264#36904264), though this question is not an exact duplicate of that question. – Akshat Mahajan May 14 '16 at 03:16
  • Thanks, but I need run in python 2.7. I will try it if it suitable for python 2.7 – user3003466 May 14 '16 at 03:29

0 Answers0