Created
March 22, 2016 16:28
-
-
Save dinob0t/7c669f17cc597affdec8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
from Queue import Queue | |
NUMBER_THREADS = 2 | |
work = range(1,11) | |
q = Queue() | |
def queue_work(): | |
while True: | |
number = q.get() | |
if not number: | |
break | |
print number | |
q.task_done() | |
threads = [] | |
for i in range(NUMBER_THREADS): | |
worker = threading.Thread(target=queue_work) | |
threads.append(worker) | |
worker.start() | |
for i in work: | |
q.put(i) | |
q.join() | |
for i in range(NUMBER_THREADS): | |
q.put(None) | |
for t in threads: | |
t.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment