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
	try:
		while True:
			n = next( n )		# schedule coroutine
	except StopIteration:
		pass

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:			# scheduler stopped
	pass
print( "stop" )

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