PHP Asynchronous implementation

Source: Internet
Author: User
PHP itself is not asynchronous, but we often need to deal with some time-consuming operations, such as the public number pushed to all users. Tried a lot of methods are not done, each push a network request, curl every time seems to be at least a second, the number of requests is certainly not. Fscokopen Post JSON data is not resolved, has been the post data empty, please each road Master weapon, to some solution!

Reply content:

PHP itself is not asynchronous, but we often need to deal with some time-consuming operations, such as the public number pushed to all users. Tried a lot of methods are not done, each push a network request, curl every time seems to be at least a second, the number of requests is certainly not. Fscokopen Post JSON data is not resolved, has been the post data empty, please each road Master weapon, to some solution!

Time-consuming operation will not be placed on the Web, you can use PHP script to put the time-consuming operation, and then the web by executing scripts and ignoring the output to achieve the asynchronous effect, this is the way I use, if you see a better answer, remember @ I look at hahaha. Here is the sample code:

pclose(popen('/usr/local/php5/bin/php tmp.php &', 'r'));// 继续你的代码

You can use Gearman for PHP

Or swoole to deal with it.

You can use Message Queuing

Message Queuing handles time-consuming tasks, Gearman Php-resque
or fastcgifinishRequest

You can use the Redis queue to achieve a gradual push function. The implementation of the principle is relatively simple, redis execution speed quickly

Store time-consuming tasks in a database, and then write a PHP CLI process to cycle through the tasks in the database.
These time-consuming tasks should not be performed on the Web (MOD_PHP,PHP-FPM) to avoid impacting Web services.

PHP+RABBITMQ, "Male push to all users" as a separate module, each need to send a message can be placed in the message queue, and then to RABBITMQ processing

Ibid., PHP+RABBITMQ, "Male push to all users" as a separate module, each time you need to send a message can be placed in the message queue, and then handed over to RABBITMQ processing

  • 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.