source:
doc/papers/concurrency/examples/Pingpong.py@
bad9c8f
      
      | Last change on this file since bad9c8f was a573c22, checked in by , 6 years ago | |
|---|---|
| 
 | |
| File size: 731 bytes | |
| Rev | Line | |
|---|---|---|
| [1e5d0f0c] | 1 | def PingPong( name, N ): | 
| [a573c22] | 2 | partner = yield # get partner | 
| 3 | yield # resume scheduler | |
| [1e5d0f0c] | 4 | for i in range( N ): | 
| 5 | print( name ) | |
| [a573c22] | 6 | yield partner # execute next | 
| [1e5d0f0c] | 7 | print( "end", name ) | 
| [17c6c1c3] | 8 | |
| [1e5d0f0c] | 9 | def Scheduler(): | 
| [a573c22] | 10 | n = yield # starting coroutine | 
| 11 | try: | |
| 12 | while True: | |
| 13 | n = next( n ) # schedule coroutine | |
| 14 | except StopIteration: | |
| 15 | pass | |
| [17c6c1c3] | 16 | |
| [1e5d0f0c] | 17 | pi = PingPong( "ping", 5 ) | 
| 18 | po = PingPong( "pong", 5 ) | |
| [a573c22] | 19 | next( pi ) # prime | 
| 20 | pi.send( po ) # send partner | |
| 21 | next( po ) # prime | |
| 22 | po.send( pi ) # send partner | |
| [17c6c1c3] | 23 | |
| [1e5d0f0c] | 24 | s = Scheduler(); | 
| [a573c22] | 25 | next( s ) # prime | 
| [17c6c1c3] | 26 | try: | 
| [1e5d0f0c] | 27 | s.send( pi ) # start cycle | 
| [a573c22] | 28 | except StopIteration: # scheduler stopped | 
| 29 | pass | |
| [1e5d0f0c] | 30 | print( "stop" ) | 
| [17c6c1c3] | 31 | |
| 32 | # Local Variables: # | |
| 33 | # tab-width: 4 # | |
| [a573c22] | 34 | # compile-command: "python3.7 Pingpong.py" # | 
| [17c6c1c3] | 35 | # End: # | 
  Note:
 See   TracBrowser
 for help on using the repository browser.
    