¿Cómo puedo poner datos en una cola y leerlos al mismo tiempo?
Frecuentes
Visto 2,429 veces
0
i'm trying to put in a multi procesing queue some data from serial port, and reading this data fom the queue with another thread, but it seems that the queue can't be acces for read until writing task is done :/
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()
¿algunas ideas?
1 Respuestas
1
The problem here has nothing to do with queues.
Esta línea:
th1 = threading.Thread(target=self.read_serial(ser))
… calls self.read_serial(ser)
, and passes the result as the target
to run on the background thread. And this function runs forever, so nothing else ever gets to happen.
Lo que quieres hacer es pass the method, not llamar al él.
th1 = threading.Thread(target=self.read_serial, args=[ser])
Tienes el mismo problema en worker2
, and need to fix that as well.
So, once you fix that, and the other problems, what else do you have to do to let one thread put to the queue and the other get from the queue at the same time?
Nothing. It just works, automatically. Just call put
in one thread, and get
in the other. If there's nothing to get yet in the reader thread, the get
will just block and not return until the other thread does a put
, and then it will immediately wake up.
Aquí is an example that's as similar as possible to your starting code. There's an even simpler example in the Queue
documentación.
Respondido el 12 de Septiembre de 13 a las 01:09
it doesnt work, just for debug i print a cola.qsize(), just before cola.get(), befor i made the changes it prints an incremental numbre, after it prints always 0 :/ - TadeoArmenta
What is the "it doesn't work"? If you need help with something, you have to show the code you're running, not make me guess at it. - abarnert
Mira aquí. This is basically your code, with this bug fixed, and some functions stubbed out. When I run this, it prints out 61
once/second forever. Of course the queue length is always 0 after that, because as soon as something gets posted to the queue by the other thread, I grab it and use it, but that's not a problem. - abarnert
@Dat30: I've already explained that nobody can help you if you just say things like "gives me an error". Am I supposed to guess everything you might possibly have done and tell you how to solve every single possibility? - abarnert
when i have "target=self.read_serial(ser)" it always read from serial and put the byte in to queue,i know it because it prints incremental number in qsize, with your fix it doesnt print the get, it just print the size in 0 - TadeoArmenta
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas python multithreading serial-port queue multiprocessing or haz tu propia pregunta.
Primero, ¿por qué estás usando
multiprocessing.Queue
en lugar dequeue.Queue
for threads? That shouldn't cause a problem, but it does make your code more confusing, and slower on most platforms too. - abarnertSegundo, deberías nunca write a loop like
while cola.qsize() > 0:
. As the documentation explicitly says, "Note, qsize() > 0 doesn’t guarantee that a subsequent get() will not block". The right thing to do is just callget
y manejar elEmpty
excepción. - abarnerti try first with queue.queue, but with the same result, so i read that queue.queue is designed to work with one process, that's why, but anyway the result is exactly the same :/ - TadeoArmenta
and sorry youre right about the while, is just one of my multiple attempts to get it working, but any way it never let objet getrx work, i comment the entire loop, an just put print "x" and it never prints - TadeoArmenta
You only have one process, with two threads, which is exactly what
queue.Queue
is designed to work with. You're not creating amultiprocessing.Process
orPool
anywhere, are you? Just changing random things without understanding them never makes your code better. - abarnert