2

How can I kill main thread and terminate program? Now after throwing RuntimeError main thread continue executing

def function_terminate():
    raise RuntimeError

def test():
    thr = threading.Timer(5.0, function_terminate, args=())
    thr.start()
    setup()
    sleep(6)
    thr.cancel()
    continue_code()

I can't use thread.interrupt_main() from this question because I'm using threading module not thread

I would be very thankful for your help

Community
  • 1
  • 1
Bazaka Bazaka
  • 135
  • 11

1 Answers1

0

You can os._exit(n), which causes the whole process to exit (in an unclean way). However, you usually have no reason to kill the main thread.

Nacib Neme
  • 859
  • 1
  • 17
  • 28
  • I'm writting own test framework. So, if test(or some action) executes more that test_timeout time, it should throw some Exception – Bazaka Bazaka Feb 02 '16 at 16:59
  • Why don't you put the test itself in a thread in join it with test_timeout? – Nacib Neme Feb 02 '16 at 17:00
  • I also have action_time time. Every function (I have more than 15 functions) should throw some Exception when action_time > function executes time. If I put every function in a thread it will be not nice, I think. Or not?) – Bazaka Bazaka Feb 02 '16 at 17:12