- File:
-
- 1 edited
-
doc/papers/concurrency/examples/Pingpong.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
doc/papers/concurrency/examples/Pingpong.py
r1e5d0f0c r17c6c1c3 1 def PingPong( name, N ): 2 partner = (yield) # get partner 3 yield # resume scheduler 4 for i in range( N ): 5 print( name ) 6 yield partner # execute next 7 print( "end", name ) 1 def Scheduler 2 try: 3 yield from ping(); 4 yield from pong(); 5 except StopIteration: 6 print( "Scheduler stop" ) 8 7 9 def Scheduler():10 n = (yield) # starting coroutine11 while True:12 n = next( n ) # schedule coroutine13 8 14 pi = PingPong( "ping", 5 ) 15 po = PingPong( "pong", 5 ) 16 next( pi ) # prime 17 pi.send( po ) # send partner 18 next( po ) # prime 19 po.send( pi ) # send partner 9 def pong(): 10 print( "pong" ) 11 for i in range( 10 ): 20 12 21 s = Scheduler(); 22 next( s ) # prime 13 yield from ping() 14 print( "stop pong" ) 15 16 def ping(): 17 global i 18 print( "ping" ) 19 i += 1 20 if i < 5: 21 yield from pong() 22 print( "stop ping" ) 23 24 p = ping() 23 25 try: 24 s.send( pi ) # start cycle26 next( p ) 25 27 except StopIteration: 26 print( "scheduler stop" ) 27 print( "stop" ) 28 print( "stop" ) 28 29 29 30 # Local Variables: # 30 31 # tab-width: 4 # 31 # compile-command: "python3.5 Pingpong.py" #32 # compile-command: "python3.5 pingpong.py" # 32 33 # End: #
Note:
See TracChangeset
for help on using the changeset viewer.