我正在尝试将来自串行端口的一些数据放入多处理队列中,并使用另一个线程从队列中读取这些数据,但似乎在写入任务完成之前队列无法访问:/
import Queue #
cola = Queue.Queue()
s_port = '/dev/ttyUSB' + sys.argv[1]
b_rate = 9600
ser = serial.Serial(
port=s_port,
baudrate=b_rate,
timeout=1
)
class worker:
def __init__(self,cola,ser):
self.cola = cola
self.ser = ser
def read_serial(self,ser):
ser.flushInput()
while True :
inp = ser.read(size=1)
byte = inp.encode('hex')
print cola.qsize() #just for debug
self.cola.put(byte)
def go(self):
th1 = threading.Thread(target=self.read_serial, args=[ser])
th1.start()
class worker2:
def __init__(self,cola,):
self.cola = cola
self.ser = ser
def getrx(self,cola):
while True :
rx = self.cola.get()
print str(rx)
pass
def go2(self):
th2 = threading.Thread(target=self.getrx, args=[cola])
th2.start()
t = worker(cola,ser)
t.go()
t2 = worker2(cola)
t2.go2()
有任何想法吗?