PHP resident program handles Message Queuing

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

  • Related Article

    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.