Update
이라는 빈 메서드가있는 기본 클래스가 있습니다. 이 Update
메서드는 다른 양의 서브 클래스가 X만큼 상속됩니다 기본 클래스는 Update
메서드를 30 틱마다 한 번 호출합니다 (지금 당장은 루프로 처리하고 있지만 30 개마다 호출하도록 할 계획입니다). 곧 바뀔 것입니다. 각 하위 클래스에는 고유 한 지침 집합이있는 Update
메서드가 있습니다. 잘 작동합니다. 그러나 한 가지 문제가 있으며 모든 스레드가 서로 충돌합니다. 파이썬 셸에서 메시지를 출력하면 함께 섞이게됩니다.파이썬 스레드 싱크
나는 그것에 대한 연구를 해왔지만, 내가 발견 한 것으로부터 나에게서 혼란 스러울만큼 혼란 스럽다. 모두 내가하고 싶은 obj1, obj2 및 obj3 자신의 라인의 출력을 가지고 함께 분쇄하지 않습니다.
다음은 다중 스레드 잠금 객체 인 semaphore
이다 필요한 것은 내 현재 코드
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')
좋아, 지금은 강신 술을 많이하게하고 그것이 내가 lookign하고 exsatley 것입니다 같은 소리 그래서 것입니다 가정 메신저에 대한 ** 뭔가 ** 같은 '진정한 동안 : threading.Semaphore.acquire () self.Update() threading.Semaphore.release()' 그러나 이것은 스레딩 모듈의 일부입니다. _thread 모듈에 apposed 됨. 그리고 사이트에서 나는 스레딩 모듈을 연구하여 처음부터 혼란 스러웠습니다. 그래서 저는 이전 _thread 모듈을 사용하고 있습니다. – skyzzle
'_thread' 만 사용하고 싶다면'_thread.allocate_lock()'을 사용할 수 있습니다. 결과물 인 lock 객체는'acquire'와'release' 메소드도 가지고 있습니다. –