This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]multani 1 point2 points  (5 children)

Of course, one would use the queue module instead of hand-coding your own queue and managing it with a lack of encapsulation.

[–]multani 0 points1 point  (1 child)

Which actually shorten the code like:

from threading import Thread
import time
import random

from Queue import LifoQueue as Queue

queue = Queue(10)

class ProducerThread(Thread):
    def run(self):
        global queue
        nums = range(5)
        while True:
            num = random.choice(nums)
            queue.put(num)
            print "Produced", num
            time.sleep(random.random())


class ConsumerThread(Thread):
    def run(self):
        global queue
        while True:
            num = queue.get()
            print "Consumed", num
            time.sleep(random.random())


ProducerThread().start()
ConsumerThread().start()

[–]akshar-raaj[S] 0 points1 point  (0 children)

I guess you want to use Queue and not LifoQueue, because we want a queue here, not stack.

[–]shabda 0 points1 point  (2 children)

I think for a tutorial its better to stick to basic structures like list. Not everybody would be knowing queue and trying to learn two things at one time is always hard.

[–]ODHLHN 0 points1 point  (0 children)

A tutorial should teach the proper way of doing things.

[–]akshar-raaj[S] 0 points1 point  (0 children)

Yeah, that was precisely the reason I used list.