See an articlePHP Uses coroutine to Achieve Cooperative Multitasking, about the realization of coroutine through php’s yield. However, the article was too long to read. I looked at it roughly.
When will PHP’s coroutine be used in the project?
Or is there a suitable scenario?
Is there a library that encapsulates it?
Coroutine can be used to make asynchronous network IO non-blocking.
For example, if you are in an http request and you need to request an external interface, there will be the following scenario.
Your front server is nginx, nginx is non-blocking asynchronous, but php-fpm is blocking synchronously. Then this task will be blocked when you request an external interface. (It is added here that the Worker process of php-fpm is blocked synchronously)
Assuming you use coroutine, when you request an interface, your task can be suspended and the context saved. Then, when your interface returns, reset the task to continue. You don’t have to spend time on this task, you can handle other http requests. Is this a high concurrency?
Similarly, when your request is querying the database, it is also an IO request and is synchronized. Coroutine can make your IO programming asynchronous and non-blocking, thus increasing your concurrency. This is mainly to allow your Central Processor to deal with other things while waiting for IO. All IO requests can be made through coroutine.
I have read your article and it is a very good one. Here the author uses yield to implement a task scheduling, which is to do the things I said above. You can also look at the translation by Brother Bird.http://www.laruence.com/2015/ …, can be said to be a supplement to the translation you see. He realized an asynchronous non-blocking server through yield, of course it is only a demo. One example should look better.