The running process of next and send in the Python generator (detailed description) and pythongenerator
For a normal generator, the first next call is equivalent to starting the generator. It starts to execute the first line of code of the generator function until the first execution of the yield statement (row 4th) is complete, jump out of the generator function.
Next, call the second one. After Entering the generator function, run the following yield Statement (row 5th) and then run the yield statement again. After the execution, the generator function is displayed, call next again, and so on.
The following is a column:
def consumer(): r = 'here' for i in xrange(3): yield r r = '200 OK'+ str(i) c = consumer() n1 = c.next() n2 = c.next() n3 = c.next()
I learned how to makeYieldAfter the function is executed, let's look at another very important function send (msg ). In fact, next () and send () play a similar role in a certain sense. The difference is that send () can be passed.YieldBut next () cannot pass a specific value. It can only pass None. Therefore, we can see that c. next () and c. send (None) have the same effect.
Please note that when calling for the first time, please use the next () statement or send (None). You cannot use send to send a non-None value; otherwise, an error will occur because noPythonYieldTo receive the value.
The following describes the sequence of sending execution. When sending (None) for the first time (corresponding to 11 rows), start the generator and run the code from the first line of the generator function until the first execution of yield (corresponding to 4th rows, jump out of the generator function. In this process, n1 has never been defined.
When sending (1) is run below, enter the generator function. Note that it is different from calling next. The execution starts from line 1 and assigns 1 to n1, but does not execute the yield part. Next we will continue to execute the next yield statement, and then re-run the yield statement. After the execution, we will jump out of the generator function.
That is to say, when sending is compared with next, only one assignment operation is started, and other running processes are the same.
def consumer(): r = 'here' while True: n1 = yield r if not n1: return print('[CONSUMER] Consuming %s...' % n1) r = '200 OK'+str(n1) def produce(c): aa = c.send(None) n = 0 while n < 5: n = n + 1 print('[PRODUCER] Producing %s...' % n) r1 = c.send(n) print('[PRODUCER] Consumer return: %s' % r1) c.close() c = consumer() produce(c)
Running result:
[PRODUCER] Producing 1...[CONSUMER] Consuming 1...[PRODUCER] Consumer return: 200 OK1[PRODUCER] Producing 2...[CONSUMER] Consuming 2...[PRODUCER] Consumer return: 200 OK2[PRODUCER] Producing 3...[CONSUMER] Consuming 3...[PRODUCER] Consumer return: 200 OK3[PRODUCER] Producing 4...[CONSUMER] Consuming 4...[PRODUCER] Consumer return: 200 OK4[PRODUCER] Producing 5...[CONSUMER] Consuming 5...[PRODUCER] Consumer return: 200 OK5
The running process of the next and send commands of the Python generator (detailed description) is all the content that I have shared with you. I hope to give you a reference and support for the help house.