Changeset 7030dab for doc/papers/concurrency/examples/ProdCons.py
- Timestamp:
- Apr 6, 2020, 4:46:28 PM (6 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum, stuck-waitfor-destruct
- Children:
- e3bc51c
- Parents:
- 71d6bd8 (diff), 057298e (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)links above to see all the changes relative to each parent. - File:
-
- 1 edited
-
doc/papers/concurrency/examples/ProdCons.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
doc/papers/concurrency/examples/ProdCons.py
r71d6bd8 r7030dab 1 1 def Prod( N ): 2 cons = (yield)# get cons3 yield # resume scheduler2 cons = yield # get cons 3 yield # resume scheduler 4 4 for i in range( N ): 5 5 print( "prod" ) 6 yield cons # execute next6 yield cons # execute next 7 7 print( "end", "prod" ) 8 8 9 9 def Cons( N ): 10 prod = (yield)# get prod11 yield # resume scheduler10 prod = yield # get prod 11 yield # resume scheduler 12 12 for i in range( N ): 13 13 print( "cons" ) 14 yield prod # execute next14 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 ) # prime25 prod.send( cons ) # send cons26 next( cons ) # prime27 cons.send( prod ) # send prod27 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 ) # prime33 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.