source: doc/papers/concurrency/examples/Pingpong.py @ daf4c89

Last change on this file since daf4c89 was a573c22, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

example programs updated for concurrency paper

  • Property mode set to 100644
File size: 731 bytes
RevLine 
[1e5d0f0c]1def 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]9def 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]17pi = PingPong( "ping", 5 )
18po = PingPong( "pong", 5 )
[a573c22]19next( pi )                                              # prime
20pi.send( po )                                   # send partner
21next( po )                                              # prime
22po.send( pi )                                   # send partner
[17c6c1c3]23
[1e5d0f0c]24s = Scheduler();
[a573c22]25next( s )                                               # prime
[17c6c1c3]26try:
[1e5d0f0c]27        s.send( pi )                            # start cycle
[a573c22]28except StopIteration:                   # scheduler stopped
29        pass
[1e5d0f0c]30print( "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.