Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/papers/concurrency/examples/ProdCons.py

    ra573c22 r1e5d0f0c  
    11def Prod( N ):
    2         cons = yield                            # get cons
    3         yield                                           # resume scheduler
     2        cons = (yield)              # get cons
     3        yield                       # resume scheduler
    44        for i in range( N ):
    55                print( "prod" )
    6                 yield cons                              # execute next
     6                yield cons              # execute next
    77        print( "end", "prod" )
    88
    99def Cons( N ):
    10         prod = yield                            # get prod
    11         yield                                           # resume scheduler
     10        prod = (yield)              # get prod
     11        yield                       # resume scheduler
    1212        for i in range( N ):
    1313                print( "cons" )
    14                 yield prod                              # execute next
     14                yield prod              # execute next
    1515        print( "end", "cons" )
    1616
    1717def 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
    2421
    2522prod = Prod( 5 )
    2623cons = Cons( 5 )
    27 next( prod )                                    # prime
    28 prod.send( cons )                               # send cons
    29 next( cons )                                    # prime
    30 cons.send( prod )                               # send prod
     24next( prod )                    # prime
     25prod.send( cons )               # send cons
     26next( cons )                    # prime
     27cons.send( prod )               # send prod
    3128
    3229s = Scheduler();
    33 next( s )                                               # prime
     30next( s )                       # prime
    3431try:
    3532        s.send( prod )                          # start cycle
    36 except StopIteration:                   # scheduler stopped
    37         pass
     33except StopIteration:
     34        print( "scheduler stop" )
    3835print( "stop" )
    3936
    4037# Local Variables: #
    4138# tab-width: 4 #
    42 # compile-command: "python3.7 ProdCons.py" #
     39# compile-command: "python3.5 ProdCons.py" #
    4340# End: #
Note: See TracChangeset for help on using the changeset viewer.