import time
import operator
from stream import ThreadedFeeder, ForkedFeeder, map, reduce

"""
Demonstrate the use of a feeder to minimize time spent by the whole pipeline
waiting for a blocking producer.
"""

def blocking_producer():
	n = 0
	while 1:
		time.sleep(0.05)
		n += 1
		if n < 100:
			yield 42
		else:
			raise StopIteration

if __name__ == '__main__':
	f = lambda x: x**x**3
	import sys
	try:
		if sys.argv[1] == '-s':
			## use a single thread
			blocking_producer() >> map(f) >> reduce(operator.add)
		elif sys.argv[1] == '-t':
			## use a feeder in a separate thread
			ThreadedFeeder(blocking_producer) >> map(f) >> reduce(operator.add)
	except IndexError:
		## use a feeder in a child process
		ForkedFeeder(blocking_producer) >> map(f) >> reduce(operator.add)

@@ -197,16 +197,303 @@
 oducer.%0A
+%0A$ time python ./feeder.py%09%09# use processes%0A%0Areal    0m7.186s%0Auser    0m7.026s%0Asys     0m0.033s%0A%0Atime python ./feeder.py -t%09# use threads%0A%0Areal    0m7.231s%0Auser    0m7.046s%0Asys     0m0.020s%0A%0A$ time python ./feeder.py -s%09# sequential%0A%0Areal    0m13.072s%0Auser    0m7.596s%0Asys     0m0.067s%0A%0A
 %22%22%22%0A%0Adef

Add timing results for example/feeder.py