Created
May 10, 2015 14:52
-
-
Save rigid/9d2b5f7e6cba3760e6a6 to your computer and use it in GitHub Desktop.
proper interruption with ctrl+c
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
#!/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