I am looking for a way to raise an exception in a while True loop. The signal to raise the exception origins in a thread t_run which continuously checks a global boolean variable called pingresponse. As soon as pingresponse equals "False" the while True loop should immediately be interrupted. What I don't want is to continuously check the variable pingresponse in the while True loop to check if the exception has to be raised.
My draft so far looks as follows:
import time
import threading
import random
def run():
# this thread continuously checks the pingresponse
global pingresponse
while True:
# simulating a random pingresponse
if random.random() > 0.8:
pingresponse = False
else:
pingresponse = True
time.sleep(0.001)
pingresponse = True
t_run = threading.Thread(target=run)
t_run.start()
while True:
i = 0
while True:
try:
print('While True loop iteration', i)
print('pingresponse:' ,pingresponse)
i += 1
# "do some work" which includes several consecutive and encapsulated while and for loops
time.sleep(1) # simulate "do some work" and prevent infinite looping if executed
# What I want is immediately interrupting the inner while True loop by raising an exception
# as soon as pingresponse was set to False in the t_run thread
# The exception should be raised independently of the current position in "do some work"
# What I don't want is to check the pingresponse variable all the time in "do some work":
# if not pingresponse:
# raise Exception
except Exception:
print('pingresponse was set to False in the t_run thread')
print('start while True loop again with iteration 0')
break