Skip to content

Instantly share code, notes, and snippets.

@basilboli
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save basilboli/4355f7ec18ded508c864 to your computer and use it in GitHub Desktop.
Save basilboli/4355f7ec18ded508c864 to your computer and use it in GitHub Desktop.
python threading sample
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