golang - What are the usage scenarios of using yield to implement coroutines in php?
某草草
某草草 2017-07-03 11:40:23
0
3
1618

I saw an article about PHP using coroutines to implement cooperative multitasking. It talked about the implementation of coroutines through php's yield. But the article was too long and I didn’t finish it, so I briefly read it.

  • When will PHP's coroutine be used in the project?

  • Or is there any applicable scenario?

  • Is there currently a library that encapsulates it?

某草草
某草草

reply all(3)
Ty80

Coroutines can be used in asynchronous network IO to make them non-blocking.

For example, if you are in an http request and you need to request an external interface, then the following scenario will occur.

Your front-end server is nginx. nginx is non-blocking and asynchronous, but php-fpm is synchronous and blocking. Then when you request the external interface, the task will be blocked. (It should be added here that the Worker process of php-fpm is blocked synchronously)

Suppose you use coroutines, then when you are requesting the interface, your task can be paused and the context saved. Then, when your interface returns, re-initiate the task and continue execution. Your process does not have to spend time on this task and can handle other http requests. Does this mean the amount of concurrency is high?

Similarly, when your request is querying the database, it is also an IO request and is also synchronous. Coroutines can make your IO programming asynchronous and non-blocking, thereby increasing your concurrency. This mainly allows your CPU to handle other things while waiting for IO. All IO requests can do this through coroutines.

I have read this article of yours, it is a very good article. Here the author uses yield to implement a task schedule, which is to do the things I mentioned above. You can also take a look at http://www.laruence.com/2015/... translated by Brother Niao, which can be said to be a supplement to the translation you saw. He implemented an asynchronous non-blocking server through yield, which is of course just a demo. An example would look better.

仅有的幸福

What I have encountered so far is that when the laravel framework model uses a cursor to fetch data, it uses yield to prevent the problem of insufficient memory caused by fetching too much data at one time

某草草

The most basic thing is to import or export a large amount of data to excel. Assuming that the export/export process needs to process the data, it can be used at this time.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template