source: doc/papers/concurrency/examples/ProdCons.py @ 1e5d0f0c

arm-ehcleanup-dtorsjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-expr
Last change on this file since 1e5d0f0c was 1e5d0f0c, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

start rewrite of coroutine section

  • Property mode set to 100644
File size: 1002 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        while True:
20                n = next( n )           # schedule coroutine
21
22prod = Prod( 5 )
23cons = Cons( 5 )
24next( prod )                    # prime
25prod.send( cons )               # send cons
26next( cons )                    # prime
27cons.send( prod )               # send prod
28
29s = Scheduler();
30next( s )                       # prime
31try:
32        s.send( prod )                          # start cycle
33except StopIteration:
34        print( "scheduler stop" )
35print( "stop" )
36
37# Local Variables: #
38# tab-width: 4 #
39# compile-command: "python3.5 ProdCons.py" #
40# End: #
Note: See TracBrowser for help on using the repository browser.