- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/papers/concurrency/examples/ProdCons.py
r1e5d0f0c ra573c22 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 while True: 20 n = next( n ) # schedule coroutine 18 n = yield # starting coroutine 19 try: 20 while True: 21 n = next( n ) # schedule coroutine 22 except StopIteration: 23 pass 21 24 22 25 prod = Prod( 5 ) 23 26 cons = Cons( 5 ) 24 next( prod ) 25 prod.send( cons ) 26 next( cons ) 27 cons.send( prod ) 27 next( prod ) # prime 28 prod.send( cons ) # send cons 29 next( cons ) # prime 30 cons.send( prod ) # send prod 28 31 29 32 s = Scheduler(); 30 next( s ) 33 next( s ) # prime 31 34 try: 32 35 s.send( prod ) # start cycle 33 except StopIteration: 34 p rint( "scheduler stop" )36 except StopIteration: # scheduler stopped 37 pass 35 38 print( "stop" ) 36 39 37 40 # Local Variables: # 38 41 # tab-width: 4 # 39 # compile-command: "python3. 5ProdCons.py" #42 # compile-command: "python3.7 ProdCons.py" # 40 43 # End: #
Note: See TracChangeset
for help on using the changeset viewer.