source: doc/papers/concurrency/examples/ProdCons.py @ 53a49cc

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

example programs updated for concurrency paper

  • Property mode set to 100644
File size: 871 bytes
Line 
1def Prod( N ):
2        cons = yield                            # get cons
3        yield                                           # resume scheduler
4        for i in range( N ):
5                print( "prod" )
6                yield cons                              # execute next
7        print( "end", "prod" )
8
9def Cons( N ):
10        prod = yield                            # get prod
11        yield                                           # resume scheduler
12        for i in range( N ):
13                print( "cons" )
14                yield prod                              # execute next
15        print( "end", "cons" )
16
17def Scheduler():
18        n = yield                                       # starting coroutine
19        try:
20                while True:
21                        n = next( n )           # schedule coroutine
22        except StopIteration:
23                pass
24
25prod = Prod( 5 )
26cons = Cons( 5 )
27next( prod )                                    # prime
28prod.send( cons )                               # send cons
29next( cons )                                    # prime
30cons.send( prod )                               # send prod
31
32s = Scheduler();
33next( s )                                               # prime
34try:
35        s.send( prod )                          # start cycle
36except StopIteration:                   # scheduler stopped
37        pass
38print( "stop" )
39
40# Local Variables: #
41# tab-width: 4 #
42# compile-command: "python3.7 ProdCons.py" #
43# End: #
Note: See TracBrowser for help on using the repository browser.