I am building a monitoring system where I once start the application, it will continuously monitor the changes and output the results to the database. For this, I used a while loop for continuous monitoring for the repetitive tasks. Here, I am using a class-level variable as 'True' for the condition in while loop. Here I am stuck with how can I change the flag to 'False' when the application is running in while loop.
Sample code looks as follow:
class Monitor:
def __init__(self):
self.monitor=True
def start(self):
i=0
while(self.monitor):
i+=1
I am running the below lines to run the code in the command line:
>>> from StartStopMontoring import Monitor
>>> m=Monitor()
>>> m.start()
^CTraceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Volumes/DATA/Innominds/WorkSpace/Python/StartStopMontoring.py", line 7, in start
while(self.monitor):
KeyboardInterrupt
>>>
Second-line creates the object and the third line calls the function, then I am unable to set the flag to false since it is in while loop with always true condition is met. (I had to interrupt the command line to stop the application).
Precisely, How can I set a class level flag to false when while loop is running?
Note: This flag changes happen from external input like user want to stop the system but not when conditions met internally within the loop.