Skip to content

Instantly share code, notes, and snippets.

@stefpe
Last active February 13, 2019 09:57
Show Gist options
  • Select an option

  • Save stefpe/165e077ff2f89fe526992f4f44c07580 to your computer and use it in GitHub Desktop.

Select an option

Save stefpe/165e077ff2f89fe526992f4f44c07580 to your computer and use it in GitHub Desktop.
queue based threadings
import queue
import threading
NUM_THREADS = 10
def worker(channel):
while True:
value = channel.get()
if value is False:
break
print('value = %d' % value)
channel.task_done()
channel = queue.Queue()
print("start workers")
workers = []
for val in range(NUM_THREADS):
th = threading.Thread(target=worker, args=(channel,))
th.start()
workers.append(th)
for input in range(100):
channel.put(input)
channel.join()
for i in range(NUM_THREADS):
channel.put(False)
for t in workers: t.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment