What is yield?

Source: Internet
Author: User
0 reply content: this is the way php implements coroutine.

To understand coroutine, you must first understand that the code is code and the function is function. The code in the function package gives this code additional meaning: There are parameters and return values. When the function calls another function, it must wait for this function to return, this constitutes a post-import, first-out, that is, a stack.

The coroutine package code is not a function and does not fully comply with the additional meanings of the function. When the coroutine runs to a certain point, yield instead of return, the execution will continue at the last yeild point.

Therefore, Ctrip violates the code execution methods identified by the general operating system and x86 cpu, that is, the stack execution method, and requires the runtime environment (such as php, python yield, and golang goroutine) schedule it by yourself to implement the semantics of the code execution you require.

Specifically, a php function containing yeild is a coroutine. It has a staged settlement value of yield $ var, but the Code does not return it. After receiving this value, the php dispatcher, feed a generator. generator is an instance that implements the + and collaborative communication interfaces (such as the send method) of the iterator interface, so it can be used in the for loop (another interface is responsible for collaborative communication ). After receiving the phase value of the coroutine, gnenerator feeds it to the for loop. When the for loop is in the next loop, it starts the coroutine again, the coroutine continues execution from the last interrupted point, continues the calculation, continues the yeild value to the generator, and the generator feeds the for loop, continues the loop until the coroutine is completed. Yield is used as a generator, that is, when yield is used as a statement. When yield is used as an expression, it is the "coroutine" usage. For details, see: PHP uses a collaborative program to implement multi-task cooperation. A function with the yield keyword is called differently from a normal function. Instead of executing the function code, the function returns a generator object, which is an iteratable object with the next () method, when the next method is called and executed to yield, It is paused and a calculated value (similar to return) is returned. When the next () method is called again, then, the following yield code is executed cyclically until yield is encountered again, and the value of the next cyclic calculation is returned... In this case, the value is not dynamically generated. One time does not occupy the memory. When I encounter this word, I probably tell the program that I want to generate a generator object and return it. Chinese Document PHP: generator Overview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.