Using python threading, is it possible to interrupt the main thread without terminate it and ask it to do something else?
For example,
def watch():
while True:
sleep(10)
somethingWrong = check()
if somethingWrong:
raise Exception("something wrong")
try:
watcher = threading.Thread(target=watch)
watcher.start()
doSomething() # This function could run several days and it cannot detect something wrong within itself, so I need the other thread watcher to check if this function perform well. In case of malfunction, the watcher should interrupt this function and ask the main thread to doSomethingElse
except:
doSomethingElse()
In this program the main thread is not affected when an exception is raised in the thread watcher
and keep doSomething
. I want the exception raised in the child thread to propogate to the main thread and make the main thread doSomethingElse
.How can I do this?
Please note that many questions similar to this and asked on this platform are in fact irrelevant. In those cases, the main thread is waiting for the message from the child thread. But in my case, the main thread is doing something. The reason for which those anwsers are not applicable here is that the main thread cannot do something and listen to the child thread at the same time.