The PHP resident program processes the message queue.

Source: Internet
Author: User
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.SleepIn 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.