Python线程同步

时间:2016-08-27 11:57:38

标签: python multithreading class

我有一个基类,它有一个名为Update的空方法。 此Update方法由X个不同的子类继承 基类每30个时钟调用一次Update方法(现在它不是。它现在正在循环中执行它,但我计划每30个时钟调用它一次)不久。) 每个子类都有自己的方法Update,并有自己的一组指令。 它工作正常。然而,存在一个问题,即所有线程都在冲突。当他们在python shell中打印出一条消息时,它们会混合在一起。

我已经做了一些研究,但是从我发现它让我感到困惑。我想做的就是让obj1,obj2和obj3的输出拥有自己的线条,而不是一起粉碎。

这是我当前的代码

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')

1 个答案:

答案 0 :(得分:1)

您需要的是semaphore,它是一个多线程锁定对象。 https://en.wikipedia.org/wiki/Semaphore_(programming)

您有时会在幼儿园或学前班学校看到相同的原则,您需要带上项链或其他物品来指示厕所没有被占用。

信号量对象有两个传统上称为PV的操作。 P操作请求锁定。如果当前正在进行锁定,则线程将一直等到锁定空闲。 V操作将释放锁,允许另一个线程获得锁。 PV是荷兰语“plaatsen”en“vrijgeven”(“put”和“release”)的缩写。

在python中,您可以使用threading.Semaphore()_thread.request_lock()工厂函数创建信号量对象。生成的对象有两种方法:acquire(= P)和release(= V)。

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