from queue import Queue, EmptyQueueError

buf = Queue()

def producer(n):
	for i in range(n) :
		buf.enqueue(i);
		yield None
		
def consumer(n):
	for i in range(n) :
		try :
			print buf.dequeue();
		except EmptyQueueError :
			pass
		yield None

def scheduler():
	threads=[]
	threads.append(producer(8))
	threads.append(producer(6))
	threads.append(consumer(10))
	i = 0
	while len(threads)>0 :
		try :
			threads[i].next()
			i=(i+1)%len(threads)
		except StopIteration :
			del threads[i]
			if len(threads)>0: 
				i=i%len(threads)
	print buf
	return

if __name__ == '__main__' :
	scheduler()
