How to reside in a PHP script, but do not adopt while (true) this way.
Reply content:
How to reside in a PHP script, but do not adopt while (true) this way.
You do not want to take the while...true way is because this method will be very CPU-intensive, if you want to directly solve the simple, with @ Lan Hao is input said sleep
, in fact, with Redis to solve is also very simple
while (true) { // block 10秒钟来等待下一个任务 $task = $redis->brPop('task', 10); if (!empty($task)) { ... }}
You have not encountered the PHP resident background runtime, memory is not released problems?!!
Use PHP's CLI mode, but you can also use while (true) with sleep, how good the combination is not used
Processing Message Queuing can execute PHP with crontab timing
If you don't use the while in this way, you can only use third-party libraries
If the queue you are listening to supports the AMQP protocol, you can install the AMQP extension, where the consume method can block listening, which is implemented by C, so it must be much faster than PHP
In the case of Redis queues, there is also a way to respond in the Phpredis extension, because Redis supports block.