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

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since d7a02ae was 1e5d0f0c, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

start rewrite of coroutine section

  • Property mode set to 100644
File size: 826 bytes
RevLine 
[1e5d0f0c]1def 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]9def Scheduler():
10        n = (yield)                 # starting coroutine
11        while True:
12                n = next( n )           # schedule coroutine
[17c6c1c3]13
[1e5d0f0c]14pi = PingPong( "ping", 5 )
15po = PingPong( "pong", 5 )
16next( pi )                      # prime
17pi.send( po )                   # send partner
18next( po )                      # prime
19po.send( pi )                   # send partner
[17c6c1c3]20
[1e5d0f0c]21s = Scheduler();
22next( s )                       # prime
[17c6c1c3]23try:
[1e5d0f0c]24        s.send( pi )                            # start cycle
[17c6c1c3]25except StopIteration:
[1e5d0f0c]26        print( "scheduler stop" )
27print( "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.