Last active
August 29, 2015 14:06
-
-
Save basilboli/4355f7ec18ded508c864 to your computer and use it in GitHub Desktop.
python threading sample
This file contains 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 Queue | |
import threading | |
import multiprocessing | |
threads_count = 10 | |
q = Queue.Queue() | |
def sum(): | |
yet_not_finished = True | |
while yet_not_finished: | |
try: | |
item = q.get(False) | |
sum = int(item) + int(item) | |
print "%i" % sum | |
q.task_done() | |
except Queue.Empty: | |
yet_not_finished = False | |
if __name__ == '__main__': | |
for i in range(55): | |
q.put(i) | |
for i in range(threads_count): | |
t = threading.Thread(target=sum) | |
t.daemon = True | |
t.start() | |
q.join() #block until all tasks are done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment