source:
doc/papers/concurrency/examples/Pingpong.py
@
e15683e
Last change on this file since e15683e was 1e5d0f0c, checked in by , 5 years ago | |
---|---|
|
|
File size: 826 bytes |
Rev | Line | |
---|---|---|
[1e5d0f0c] | 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 ) | |
[17c6c1c3] | 8 | |
[1e5d0f0c] | 9 | def Scheduler(): |
10 | n = (yield) # starting coroutine | |
11 | while True: | |
12 | n = next( n ) # schedule coroutine | |
[17c6c1c3] | 13 | |
[1e5d0f0c] | 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 | |
[17c6c1c3] | 20 | |
[1e5d0f0c] | 21 | s = Scheduler(); |
22 | next( s ) # prime | |
[17c6c1c3] | 23 | try: |
[1e5d0f0c] | 24 | s.send( pi ) # start cycle |
[17c6c1c3] | 25 | except StopIteration: |
[1e5d0f0c] | 26 | print( "scheduler stop" ) |
27 | print( "stop" ) | |
[17c6c1c3] | 28 | |
29 | # Local Variables: # | |
30 | # tab-width: 4 # | |
[1e5d0f0c] | 31 | # compile-command: "python3.5 Pingpong.py" # |
[17c6c1c3] | 32 | # End: # |
Note: See TracBrowser
for help on using the repository browser.