Skip to content

Instantly share code, notes, and snippets.

@rigid
Created May 10, 2015 14:52
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 rigid/9d2b5f7e6cba3760e6a6 to your computer and use it in GitHub Desktop.
Save rigid/9d2b5f7e6cba3760e6a6 to your computer and use it in GitHub Desktop.
proper interruption with ctrl+c
#!/usr/bin/python
import Queue
import threading
import commands
def run_delay(delay):
try:
print "Start delaying: {0}".format(delay)
result = commands.getoutput("sleep {0} && echo {0}".format(delay))
print "Done delaying: {0}".format(result)
return result
except:
print "Error sleeping {0}".format(delay)
return None
def worker():
while True:
delay = q.get()
if delay is None:
return
run_delay(delay)
q.task_done()
##############
num_threads=5
q = Queue.Queue()
for i in range(num_threads):
t = threading.Thread(target=worker)
t.daemon = True
t.start()
for item in [ 10, 30, 2, 4, 8, 1, 4, 2, 8, 3 ]:
q.put(item)
q.join()
print "All gone"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment