3

I've got this piece of code that starts a thread. Then it waits for a few seconds and then it checks if it got a Event. If it does have an event, the thread is 'canceled'. Else a Exception is thrown.

I want to know how to catch this Exception because I've searched for a long time and did not find a clear answer.

import threading
import time

def thread(args1, stop_event):
    print "starting thread"
    stop_event.wait(10)
    if not stop_event.is_set():
        raise Exception("signal!")
    pass

try:
    t_stop = threading.Event()
    t = threading.Thread(target=thread, args=(1, t_stop))
    t.start()

    time.sleep(11)

    #normally this should not be executed
    print "stopping thread!"
    t_stop.set()

except Exception as e:
    print "action took to long, bye!"

First I've tried this concept with a Python signal but when performing more than 1 signal.alarm, it just got stuck for no reason at all (prob. a bug).

EDIT:
I don't want to extend the already existing class, I want to work with the native defined class.

Also I do not want to loop continuously to check if exceptions occured. I want the thread to pass the exception to its parent method. Therefore the time.sleep(11) action in my code

Jonas Libbrecht
  • 768
  • 1
  • 8
  • 17
  • Possible duplicate of [Catch a thread's exception in the caller thread in Python](http://stackoverflow.com/questions/2829329/catch-a-threads-exception-in-the-caller-thread-in-python) – K L Feb 16 '16 at 16:02
  • I don't want to extend the already existing class, I want to work with the native defined class. – Jonas Libbrecht Feb 16 '16 at 16:05
  • Also I do not want to loop continuously to check if exceptions occured. I want the thread to pass the exception to its parent method. Therefore the `time.sleep(11)` action in my code – Jonas Libbrecht Feb 16 '16 at 16:11
  • You can make a Queue, pass it to the thread as an argument, then check the queue after your 11-second sleep. – K L Feb 16 '16 at 16:19
  • The 11 seconds sleep is an example of an argument taking to long to complete. For instance, this can be 100 seconds or even more. Please read the part of the `signal` and you will prob. understand better – Jonas Libbrecht Feb 16 '16 at 16:24

1 Answers1

4

Created from my comments on your question.
Try something like this.

import sys
import threading
import time
import Queue


def thread(args1, stop_event, queue_obj):
    print "starting thread"
    stop_event.wait(10)
    if not stop_event.is_set():
        try:
            raise Exception("signal!")
        except Exception:
            queue_obj.put(sys.exc_info())
    pass


try:
    queue_obj = Queue.Queue()
    t_stop = threading.Event()
    t = threading.Thread(target=thread, args=(1, t_stop, queue_obj))
    t.start()

    time.sleep(11)

    # normally this should not be executed
    print "stopping thread!"
    t_stop.set()

    try:
        exc = queue_obj.get(block=False)
    except Queue.Empty:
        pass
    else:
        exc_type, exc_obj, exc_trace = exc
        print exc_obj

except Exception as e:
    print "action took to long, bye!"

When run, the Exception "signal!" is raised, and is printed by print exc_obj.

K L
  • 286
  • 2
  • 7