Python - How to use multi threading with queue when you don't know how many times you need to

Submitted 3 years, 4 months ago
Ticket #326
Views 253
Language/Framework Python
Priority Low
Status Closed

 How to use multi threading with queue

Submitted on Dec 18, 20
add a comment

1 Answer

Verified

from Queue import Queue
from threading import Thread

def do_stuff(q):
  while True:
    print q.get()
    q.task_done()

q = Queue(maxsize=0)
num_threads = 10

for i in range(num_threads):
  worker = Thread(target=do_stuff, args=(q,))
  worker.setDaemon(True)
  worker.start()

for x in range(100):
  q.put(x)

q.join()

Submitted 3 years, 3 months ago


Latest Blogs