직렬 연결에서 데이터를 생성하고 다른 소비자 스레드가 사용할 여러 대기열에 넣는 제작자 스레드가 있습니다. 그러나, 생산자 스레드가 이미 실행을 시작한 후에 주 스레드에서 추가 대기열 (추가 소비자)을 추가 할 수 있기를 원합니다.파이썬에서 실행중인 스레드에서 메소드를 변경하거나 메소드를 호출하는 방법은 무엇입니까?
e.e. 아래의 코드에서이 스레드가 실행되는 동안 주 스레드에서 listOfQueues에 대기열을 추가하는 방법은 무엇입니까? addQueue (newQueue)과 같은 메서드를 추가하여이 클래스에 추가하면 listOfQueues이 될까요? thread가 run 메소드에있을 가능성이 높지는 않습니다. stop 이벤트와 비슷한 이벤트를 만들 수 있습니까?
class ProducerThread(threading.Thread):
def __init__(self, listOfQueues):
super(ProducerThread, self).__init__()
self.listOfQueues = listOfQueues
self._stop_event = threading.Event() # Flag to be set when the thread should stop
def run(self):
ser = serial.Serial() # Some serial connection
while(not self.stopped()):
try:
bytestring = ser.readline() # Serial connection or "producer" at some rate
for q in self.listOfQueues:
q.put(bytestring)
except serial.SerialException:
continue
def stop(self):
'''
Call this function to stop the thread. Must also use .join() in the main
thread to fully ensure the thread has completed.
:return:
'''
self._stop_event.set()
def stopped(self):
'''
Call this function to determine if the thread has stopped.
:return: boolean True or False
'''
return self._stop_event.is_set()
당신은'listOfQueues : q에'자기 자신을 놓쳤습니다. – 101