Php asynchronous implementation

Source: Internet
Author: User
Php itself is not asynchronous, but we often need to handle some time-consuming operations, such as pushing a public number to all users. I have tried many methods and have not done this. Each piece pushes a network request. The curl seems to be at least one second at a time, and the number of requests is too many. Fscokopenpostjson data is not... php itself is not asynchronous, but we often need to handle some time-consuming operations, such as pushing a public number to all users. I have tried many methods and have not done this. Each piece pushes a network request. The curl seems to be at least one second at a time, and the number of requests is too many. Fscokopen post json data has not been solved, and post data empty has always appeared. Please give some solutions to each of your experts!

Reply content:

Php itself is not asynchronous, but we often need to handle some time-consuming operations, such as pushing a public number to all users. I have tried many methods and have not done this. Each piece pushes a network request. The curl seems to be at least one second at a time, and the number of requests is too many. Fscokopen post json data has not been solved, and post data empty has always appeared. Please give some solutions to each of your experts!

Time-consuming operations do not need to be placed on the web. You can use php to write scripts to put time-consuming operations in the web, and then execute the script on the web and ignore the output to achieve the asynchronous effect. This is my method, if you see a better answer, remember @ I'll take a look at Hahaha. The following is the sample code:

Pclose (popen ('/usr/local/php5/bin/php tmp. php &', 'R'); // continue your code

You can use gearman for php

Or swoole.

Message Queue is available.

Message Queue processing time-consuming task, gearman php-resque
Or fastcgiFinishRequest

You can use the redis queue to implement the progressive push function. The implementation principle is relatively simple, and redis is fast to execute.

Store time-consuming tasks in the database, and then write a php cli process to process the tasks in the database cyclically.
These time-consuming tasks should not be executed on the Web (MOD_PHP, PHP-FPM) to avoid affecting Web Services.

Php + rabbitmq, "pushed to all users by the public" as a separate module, each time a message needs to be sent, it can be placed in the message queue, and all subsequent messages will be handed over to rabbitmq for processing.

Same as above, php + rabbitmq, "public number push to all users" as a separate module, each time a message needs to be sent, it can be placed in the message queue, and all subsequent messages are handed over to rabbitmq for 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.