-1

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:

  1. How can I make the function checker() accessible only to other functions in this class (limit the scope)?

  2. 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.

Manidip Sengupta
  • 3,573
  • 5
  • 25
  • 27
  • you can use **underscores** to make a variable or method private.at least one for `variables` (_somevar) and at least two for `methods` (__someMethod).see here => https://docs.python.org/3/tutorial/classes.html#private-variables – Mojtaba Kamyabi Nov 04 '17 at 06:43
  • Python doesn't really have private attributes or methods. You can use a _single_ leading underscore to mark an attribute or method private, but that's merely a convention, Python doesn't enforce any conditions on such objects. The slogan is "We're all consenting adults here". – PM 2Ring Nov 04 '17 at 06:49
  • @shotgunner I don't know where you learned that, but it's not correct. Please see [What is the meaning of a single- and a double-underscore before an object name?](https://stackoverflow.com/questions/1301346/what-is-the-meaning-of-a-single-and-a-double-underscore-before-an-object-name) – PM 2Ring Nov 04 '17 at 06:51
  • @PM2Ring oops, you're right.I've got mistake – Mojtaba Kamyabi Nov 04 '17 at 06:54
  • There are various tools to assist in synchronization available in the `threading` module. You could probably use a simple `Lock` or `Event` for this task. Also note that in recent versions of Python there are more modern alternatives to the threading module. Please see [asyncio](https://docs.python.org/3/library/asyncio.html). – PM 2Ring Nov 04 '17 at 06:55
  • Please don't roll two questions into one. How will you decide which answer to accept if one person answers question 1, and another person answers question 2? – Barmar Nov 04 '17 at 07:07

1 Answers1

0

1 - you dont. Scope of methods and variables is most of the time handled by programming conventions. In this case you would add a underline in the beggining of the checker to identify it as a private method that should only be called by the other methods inside the class. 2 - to synchronize multiple threads you need to work with the the threads using threading specific commands, trying to use the "carry_on" like you did will make you end in some weird alleys. Ps: if I recall there is the @Async and @Sync annotations, you could look further into it.

Lucas Coppio
  • 328
  • 2
  • 12
  • Thank you. "You cannot do this" is a perfectly valid answer :) – Manidip Sengupta Nov 04 '17 at 07:10
  • Python is very permissive, so we depend heavily in good pratices and conventios, pep, etc – Lucas Coppio Nov 04 '17 at 07:31
  • Messing around with my code and reading up stuff here - I figured that changing the method name from checker to __checker solves 1 problem (makes it private) - with the error: "AttributeError: 'CheckTscope' object has no attribute '__checker'". – Manidip Sengupta Nov 04 '17 at 07:59