Skip to content

Instantly share code, notes, and snippets.

@m00nlight
Created February 6, 2015 13:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m00nlight/d72f3913bab79e8e6e75 to your computer and use it in GitHub Desktop.
Save m00nlight/d72f3913bab79e8e6e75 to your computer and use it in GitHub Desktop.
Python multiple consumer and producer problem
from Queue import Queue
from threading import Thread
from random import randrange
queue = Queue(10)
class Consumer(Thread):
def __init__(self, queue):
Thread.__init__(self)
self.queue = queue
def run(self):
while True:
num = self.queue.get()
self.queue.task_done()
print 'consumer get number: ' + str(num)
class Producer(Thread):
def __init__(self, queue):
Thread.__init__(self)
self.queue = queue
def run(self):
while True:
num = randrange(1, 10)
self.queue.put(num)
print 'producer produce number : ' + str(num)
def main():
for i in range(5):
p = Producer(queue)
c = Consumer(queue)
p.setDaemon(True)
c.setDaemon(True)
p.start()
c.start()
queue.join()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment