user1423020
user1423020

Reputation: 275

Queue conflicts when multithreading?

I tried to put together a very simple multithreading model, and so far it seems to work. My question is how am I sure that two threads will not grab the same value from the queue simultaneously and give me repeats? Is there just some built in method that prevents this? I added a delay to try to put time between when each thread will get a value from the queue, is this necessary?

from Queue import Queue
from threading import Thread
import time

class myThread(Thread):
    def __init__(self,queue):
        Thread.__init__(self)
        self.queue = queue
    def run(self):       
        while True:                      
            time.sleep(0.0001)   #not sure if this is good enough to compensate for overlap in pulling the same value out of a queue         
            task = self.queue.get() #pull a number from the queue,
            if task != None:          
                out.append(task) #This will be where you        
            else:               
                break


queue = Queue()
out = []


for i in range(10):
    t = myThread(queue)
    t.start()
for i in range(100):
    queue.put(i)
print out

Upvotes: 1

Views: 774

Answers (3)

Ismael Vacco
Ismael Vacco

Reputation: 1030

You code are correct. The sleep do not is necessary. The Queue is synchronization. Exist a lock that avoid threads to pull the same task.

Upvotes: 2

Steven Huwig
Steven Huwig

Reputation: 20784

The whole point of queue.Queue is that it guarantees this property for you. No sleep is needed (and sleeping is never a good way around concurrency glitches).

Upvotes: 1

lxop
lxop

Reputation: 8595

The Queue class implements locking to prevent this happening, see http://docs.python.org/library/queue.html, in particular:

It is especially useful in threaded programming when information must be exchanged safely between multiple threads. The Queue class in this module implements all the required locking semantics.

So you don't need any delay, the queue should work exactly as you want. That's what it was written for (basically) :-)

Upvotes: 3

Related Questions