def PingPong( name, N ):
	partner = (yield)           # get partner
	yield                       # resume scheduler
	for i in range( N ):
		print( name )
		yield partner           # execute next
	print( "end", name )

def Scheduler():
	n = (yield)                 # starting coroutine
	while True:
		n = next( n )           # schedule coroutine

pi = PingPong( "ping", 5 )
po = PingPong( "pong", 5 )
next( pi )                      # prime
pi.send( po )                   # send partner
next( po )                      # prime
po.send( pi )                   # send partner

s = Scheduler();
next( s )                       # prime
try:
	s.send( pi )				# start cycle
except StopIteration:
	print( "scheduler stop" )
print( "stop" )

# Local Variables: #
# tab-width: 4 #
# compile-command: "python3.5 Pingpong.py" #
# End: #
