- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/papers/concurrency/examples/ProdCons.py
ra573c22 r1e5d0f0c 1 1 def Prod( N ): 2 cons = yield# get cons3 yield 2 cons = (yield) # get cons 3 yield # resume scheduler 4 4 for i in range( N ): 5 5 print( "prod" ) 6 yield cons 6 yield cons # execute next 7 7 print( "end", "prod" ) 8 8 9 9 def Cons( N ): 10 prod = yield# get prod11 yield 10 prod = (yield) # get prod 11 yield # resume scheduler 12 12 for i in range( N ): 13 13 print( "cons" ) 14 yield prod 14 yield prod # execute next 15 15 print( "end", "cons" ) 16 16 17 17 def Scheduler(): 18 n = yield # starting coroutine 19 try: 20 while True: 21 n = next( n ) # schedule coroutine 22 except StopIteration: 23 pass 18 n = (yield) # starting coroutine 19 while True: 20 n = next( n ) # schedule coroutine 24 21 25 22 prod = Prod( 5 ) 26 23 cons = Cons( 5 ) 27 next( prod ) 28 prod.send( cons ) 29 next( cons ) 30 cons.send( prod ) 24 next( prod ) # prime 25 prod.send( cons ) # send cons 26 next( cons ) # prime 27 cons.send( prod ) # send prod 31 28 32 29 s = Scheduler(); 33 next( s ) 30 next( s ) # prime 34 31 try: 35 32 s.send( prod ) # start cycle 36 except StopIteration: # scheduler stopped37 p ass33 except StopIteration: 34 print( "scheduler stop" ) 38 35 print( "stop" ) 39 36 40 37 # Local Variables: # 41 38 # tab-width: 4 # 42 # compile-command: "python3. 7ProdCons.py" #39 # compile-command: "python3.5 ProdCons.py" # 43 40 # End: #
Note: See TracChangeset
for help on using the changeset viewer.