i = 0 def pong(): global i print( "pong" ) if i < 4: yield from ping() print( "stop pong" ) def ping(): global i print( "ping" ) i += 1 if i < 5: yield from pong() print( "stop ping" ) p = ping() try: next( p ) except StopIteration: print( "stop" ) # Local Variables: # # tab-width: 4 # # compile-command: "python3.5 pingpong.py" # # End: #