Python threading sync
<p>I have a base class that has an empty method called <code>Update</code>.
This <code>Update</code> method is inherited by X amount of different subclasses
The base class calls the <code>Update</code> method once every 30 ticks (right now it's not. It's just doing it in a loop right now, but I plan to make it get called every 30 ticks soon.)
Each subclass has its own method <code>Update</code> with its own set of instructions.
It's working fine. However there is one problem and that is all the threads are clashing together. When they print out a message in the python shell they are blended together.</p>

<p>I have done some research in to it but from what I have found it is confusing the heck out of me. All i want to do is have the output of obj1, obj2 and obj3 own there own lines and not smashed together.</p>

<p>here is my current code</p>

<pre><code>import _thread
class BaseClass(object):
    def __init__(self, name):
        self.name = name
        _thread.start_new_thread( self.UpdateHandler,() )

    def ClassType(self):
        """Returns a string of the childs type"""
        pass

    def UpdateHandler(self):
        #this part handles when to call the update.
        #it is allso needed so that it can be run in a thread.
        while True:
            self.Update()

    def Update(self):
        #This is a place holder.
        #It stops classes that dont have a Update function crashing when called
        pass

#---------------------------------------------------------------------------------
class ChildClassA(BaseClass):
    def __init__(self, name):
        super(ChildClassA, self).__init__(name)

    def ClassType(self):
        return 'ChildClassA'

    def Update(self):
        print(self.name, ": is doing stuff CLASS A")

#----------------------------------------------------------------------------------
class ChildClassB(BaseClass):
    def __init__(self, name):
        super(ChildClassB, self).__init__(name)

    def Update(self):
        print(self.name, "This is a completley diffeent action CLASS B")
        self.Hi()

    def ClassType(self):
        return 'ChildClassB'

    def Hi(self):
        print("Hi")
#----------------------------------------------------------------------------------
class ChildClassC(BaseClass): 
    def __init__(self, name): 
        super(ChildClassC, self).__init__(name) 

    def Update(self): 
        print(self.name, "This is a completley diffeent action")

#--------------------------------------------------------------------------------

obj1 = ChildClassA('Obj1') 
obj2 = ChildClassA('Obj2')
obj3 = ChildClassB('Obj3')
</code></pre>

<p>What you need is a <code>semaphore</code>, which is a multi-threaded lock object. <a href="https://en.wikipedia.org/wiki/Semaphore_(programming)" rel="nofollow">https://en.wikipedia.org/wiki/Semaphore_(programming)</a>.</p>

<p>You sometimes see the same principle in kindergarten or pre-schools where to go to the toilet you need to take a necklace or another object to indicate the toilet is not occupied.</p>

<p>A semaphore object has two operations traditionally called <code>P</code> and <code>V</code>. The <code>P</code> operation requests the lock. If the lock is currently taken, the thread will wait until the lock becomes free. The <code>V</code> operation will free the lock, allowing another thread a chance to take the lock. <code>P</code> and <code>V</code> are abbreviations for the Dutch words "plaatsen" en "vrijgeven" ("put" and "release").</p>

<p>In python you can create a semaphore object by using the <code>threading.Semaphore()</code> or the <code>_thread.request_lock()</code> factory functions. The resulting objects have two methods: <code>acquire</code> (=<code>P</code>) and <code>release</code> (=<code>V</code>). </p>

<pre><code>import _thread
class BaseClass(object):
    lock = _thread.request_lock()
    def __init__(self, name):
        self.name = name
        _thread.start_new_thread( self.UpdateHandler,() )

    def ClassType(self):
        """Returns a string of the childs type"""
        pass

    def UpdateHandler(self):
        #this part handles when to call the update.
        #it is allso needed so that it can be run in a thread.
        while True:
            self.lock.acquire()
            self.Update()
            self.lock.release()


    def Update(self):
        #This is a place holder.
        #It stops classes that dont have a Update function crashing   when called
        pass
</code></pre>

python multithreading class