I have a doubt regarding the implementation of a logic using python. In printer(), when the value of abort changes in line 10, is there a way for main() to know it and immediately break the while loop?
EDIT: abort becomes True if and only if something went wrong.
import time
abort = False
def printer():
global abort
print("I'm the printer")
time.sleep(5)
if somethingiswrong():
print("I'm aborting here")
abort = True
time.sleep(2)
print("I have aborted")
def main():
global abort
while not abort:
print("In while loop")
time.sleep(2)
printer()
if abort:
break
print("Printer killed me")
print("Quitting")
if __name__ == "__main__":
main()
I mean, the log output is now:
> In while loop
> I'm the printer
> I'm aborting here
> I have aborted
> Quitting
Is there a more optimized way to achieve an output of:
> In while loop
> I'm the printer
> I'm aborting here
> Quitting
I'm not an expert and am not familiar with any of the python shortcuts, hacks, tricks, etc. Any help would be great..!!
abortfor? Your program has a very linear flow.printerin another thread for this to work