This a 2-question rolled into 1 - I get the following output from the script next
Checker: start of thread
Checker: ( 0 ) Hello, world
Checker: ( 1 ) Hello, world
Checker: ( 2 ) How do you do
Checker: ( 3 ) How do you do
Checker: start of thread
Checker: ( 4 ) Bye for now
Checker: exiting thread
Checker: ( 0 ) Bye for now
Checker: exiting thread
The script:
#!/usr/local/bin/python
import time
import threading
class CheckTscope:
def __init__ (self):
self.name = 'checker'
self.msg = None
def checker (self, msg="Hey stranger"):
count = 0
self.setMessage(msg)
print ("Checker: start of thread")
while True:
time.sleep (0.04)
print ("Checker: (", count, ")", self.getMessage())
if self.carry_on:
count += 1
continue
break
print ("Checker: exiting thread")
def getMessage (self):
return self.msg
def setMessage (self, text):
self.msg = text
def construct (self, initxt):
self.setMessage (initxt)
self.carry_on = True
reporter = threading.Thread(target=self.checker, args=(initxt,))
reporter.start()
def destruct (self):
self.carry_on = False
if __name__ == '__main__':
ct = CheckTscope()
ct.construct("Hello, world")
time.sleep(0.125)
ct.setMessage("How do you do")
time.sleep(0.06)
ct.destruct()
time.sleep(0.02)
ct.checker ("Bye for now")
The questions:
How can I make the function checker() accessible only to other functions in this class (limit the scope)?
How do I synchronize for multiple threads ("4 - Bye for now" is an error, counter should be reset when message is set to new value).
Essentially, I am looking for a replacement of "synchronized private void" from another language. Thank you for your help.