from threading import Thread
import time
def worker(n, emit):
time.sleep(n)
emit(n)
print(n)
def sleep_sort(s):
# Idea inspired by: http://dis.4chan.org/read/prog/1295544154
result = []
emit = result.append
threads = [Thread(target=worker, args=(x, emit)) for x in s]
for t in threads:
t.start()
for t in threads:
t.join()
return result
if __name__ == '__main__':
import random
s = list(range(10))
random.shuffle(s)
print ('Shuffled:', s)
print ('Sorted:', sleep_sort(s))
Diff to Previous Revision
--- revision 1 2011-06-16 08:26:28
+++ revision 2 2011-06-17 05:17:22
@@ -20,7 +20,7 @@
if __name__ == '__main__':
import random
- s = range(10)
+ s = list(range(10))
random.shuffle(s)
print ('Shuffled:', s)
print ('Sorted:', sleep_sort(s))