¿Cómo puedo poner datos en una cola y leerlos al mismo tiempo?

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(

class worker: 
    def __init__(self,cola,ser): 
        self.cola = cola 
        self.ser = ser 

    def read_serial(self,ser): 
        while True : 
            inp = ser.read(size=1) 
            byte = inp.encode('hex')
            print cola.qsize()  #just for debug 

    def go(self): 
        th1 = threading.Thread(target=self.read_serial, args=[ser]) 

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)

    def go2(self): 
        th2 = threading.Thread(target=self.getrx, args=[cola])

t = worker(cola,ser)
t2 = worker2(cola) 

¿algunas ideas?

preguntado el 12 de septiembre de 13 a las 00:09

Primero, ¿por qué estás usando multiprocessing.Queue en lugar de queue.Queue for threads? That shouldn't cause a problem, but it does make your code more confusing, and slower on most platforms too. -

Segundo, 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 call gety manejar el Empty excepción. -

i 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 :/ -

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 -

You only have one process, with two threads, which is exactly what queue.Queue is designed to work with. You're not creating a multiprocessing.Process or Pool anywhere, are you? Just changing random things without understanding them never makes your code better. -

1 Respuestas

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 or haz tu propia pregunta.