How to resident a php script without using the while (true) method. How to resident a php script without using the while (true) method.
Reply content:
How to resident a php script without using the while (true) method.
You don't want to use the while... true method because this method will occupy a lot of cpu. If you want to solve it directly, it's very easy.Sleep
In fact, using redis is also very easy to solve
While (true) {// block for 10 seconds to wait for the next task $ task = $ redis-> brPop ('task', 10); if (! Empty ($ task )){...}}
You have never encountered the problem that the memory cannot be released when php is running in the background ?!!!
Please use the CLI mode of php. However, you can also use while (true) with sleep directly. Why not use a good combination?
You can use crontab to periodically execute php for Message Queue processing.
If you do not use the while method, you can only use a third-party library.
If the listening queue supports the amqp protocol, you can install the amqp extension. The consume method can block the listening. This blocking is implemented by C, so it must be much faster than php.
For redis queues, phpredis extensions also have response methods, because redis supports block.