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