Hi, I am looking for a way to implement a coroutine in a php file. The idea is that I have lengthy processes that should be able to work for hours or days. Thus, other php files will call functions in the same file as coroutine to update something, and then call a function like $coroutine.process() , which forces the coroutine to continue from its last exit. This is done so as not to use a large state machine.
I think that the php file of the coroutine file will not actually be launched when it works, but at a given processing time, it will be entered from above and use something like a switch or goto to restart from the previous lesson. Then, when it reaches the next damage, the file will save its current state somewhere (for example, a session or database), and then exit.
Has anyone heard of this or a metaphor like this? Bonus points for aggregating and managing multiple coroutines under the same collection in some way, possibly with threadlike support, so that the thread continues in one place when they end (a bit like Go).
UPDATE: php 5.5.0 added support for generators and coroutines:
https://github.com/php/php-src/blob/php-5.5.0/NEWS
https://wiki.php.net/rfc/generators
I have not tried it yet, so maybe someone can offer an example with barebones. I am trying to convert a state machine into a coroutine. So, for example, the switch command inside the for loop (whose flow is difficult to observe, and the probability of error when adding more states) is converted to a cooperative flow, where each decision point is easy to see in an ordered linear flow, which is paused to change the state with the keyword yield.
A concrete example of this is that you are writing an elevator controller. Instead of deciding whether to read the state of the buttons based on the state of the elevator (STATE_RISING, STATE_LOWERING, STATE_WAITING, etc.), you write one cycle with sub-loops that are executed while the elevator is in each state. Therefore, when he rises, he does not lower, and he will not read any buttons except the emergency button. This may not seem very complicated, but in a complex state computer, such as a chat server, it is almost impossible to update the state machine without introducing subtle errors. While the version of the joint version (coroutine) has a clearly visible stream, which is easier to debug.