Php computing problems

Source: Internet
Author: User
{Code ...}
$ Res ['code'] = 200; // The generated data is calculated here, but the generated data is not urgently needed by the user. // you did not think of other methods to trigger this computing program, therefore, only the last user can be designed for computation after submission. // because of the large amount of data, the waiting time for the user to return is determined by the computing time. Can I return the data to the user before computation? How can I perform this operation? $ This-> ajaxReturn ($ res); // you only need to return code = 200 to the user.

Reply content:
$ Res ['code'] = 200; // The generated data is calculated here, but the generated data is not urgently needed by the user. // you did not think of other methods to trigger this computing program, therefore, only the last user can be designed for computation after submission. // because of the large amount of data, the waiting time for the user to return is determined by the computing time. Can I return the data to the user before computation? How can I perform this operation? $ This-> ajaxReturn ($ res); // you only need to return code = 200 to the user.

Push the computing service to mq first, that is, the task queue. This can be done using redis.
Run scripts in Linux and consume the queue all the time.

Use redis to implement a queue, put the relevant information in the queue each time, and then return the code to the user. The data in the queue is run using scripts.

Gearman

Http://gearman.org/

This is what you need.

Asynchronous Operation

Take computing as a task and put it in the queue. There is a script running, getting the task from the queue, and then processing it in the background.
This is the so-called message queue. You can check the message queues such as rabbitmq and gearman.
I have written an article on php rabbitmq usage.
Https://segmentfault.com/a/1190000004924668

As mentioned above, it is a good solution to put computing tasks in the messaging team. However, you need to set up a queue service.

If the computing workload is not huge, can it be easier to fork a process to solve it.

Refer to the onEndRequest method of the Request object in the YII framework. This method can be called asynchronously.

I agree with @ zyxcba's answer. If the data volume is small, we can first flush the data and then fork a process to process specific computing tasks. HoweverpcntlThe extension can only be used on Linux.

If you do not need to return the calculated data to the user, you can first return the data to continue executing the code. Php-fpm can use this fastcgi_finish_request, echo return value, fastcgi_finish_request (), and then continue to execute .... Or the queue is used.

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.