Examples of parallel generator python

Builder parallel instance:
send received transmission value is assigned to yield variable baozi
       #yield role just here to save the current state value and then return later in the call to the next, back to yield
       # Simply will not call the next pass to yield value, next only to call yield, send a call to pass yield and value
       
       import time
# Coroutine
def consumer(name):
    Print ( " !% S ready bun it " % name)
     the while True:
       baozi = yield #send transmitted yield value is assigned to received a variable baozi
       #yield role just here to save the current state value and then return later in the call to the next, back to yield
       # Simply will not call the next pass to yield value, next only to call yield, send a call to pass yield and value

       Print ( " bun [% s] comes, the [% s] ate! " % (Baozi, name))

# c = consumer("dapangxiaozi")
# c.__next__()
C.send # ( " leek stuffing " )
# c.__next__()

# def producer(name):
C # = Consumer ( ' A ' ) becomes generator just #
#     c2 = consumer('B')
#     c.__next__()
#     c2.__next__()
Print # ( " I began to prepare the buns do it! " )
#     for i in range(10):
#         time.sleep(1)
Print # ( " made two buns! " )
#         c.send(i)
#         c2.send(i)
#
# producer("alex")
def producer(name):
    c= consumer("A")
    c2 = consumer("B")
    c.__next__()
    c2.__next__()
    Print ( " ! start a bun " )
     for i in the Range ( 10 ):
        time.sleep(1)
        Print ( " % S to do a bun " % name)
        c.send(i)
        c2.send(i)
producer("alex")
* Can be () function returns the call to the next and continue to the next value of the object called iterators: Iterator

Guess you like

Origin www.cnblogs.com/fuyuteng/p/12091066.html