PHP calculation problems

Source: Internet
Author: User
Keywords Php
Tags rabbitmq
$res['code'] = 200;//这里进行计算生成数据,但生成的数据不是用户急需的,//没有想到其它方式可以触发这个计算程序,所以只能设计最后一个用户提交后计算,//由于数据量大,用户等待返回的时间由计算时间决定,可不可以先返回给用户再计算,怎么操作?$this->ajaxReturn($res);//只需返回 code=200 给用户

Reply content:

$res['code'] = 200;//这里进行计算生成数据,但生成的数据不是用户急需的,//没有想到其它方式可以触发这个计算程序,所以只能设计最后一个用户提交后计算,//由于数据量大,用户等待返回的时间由计算时间决定,可不可以先返回给用户再计算,怎么操作?$this->ajaxReturn($res);//只需返回 code=200 给用户

The computing business to be done is first pushed into MQ, the task queue, which can be done with Redis.
Linux runs scripts that have been consuming queues.

Use Redis to implement a queue, each time to put the relevant information in the queue, and then return to the user code, the data in the queue to run with a script


That's what you need.

Asynchronous operation

Take the calculation as a task, put it in the queue, have a script running, get the task from the queue, and then process it in the background.
This is the need for so-called Message Queuing, you can see the Rabbitmq,gearman and other Message Queuing.
I have written here a PHP rabbitmq use method, you can see.

It's a good idea to put computing tasks in the message team, as the big gods mentioned above. But you need to build a queue service.

If the computational amount is not particularly large, is it easier to fork a process to solve it?

Refer to the Onendrequest method of the request object of the YII framework, which is a method that can implement an asynchronous call.

Agree @zyxcba answer, if the volume is not enough, you can first flush out the data, and then fork a process to handle the specific computing tasks, but pcntl the extension can only be used on Linux.

If the calculated data does not need to be returned to the user, you can return to continue executing the code. PHP-FPM can use this Fastcgi_finish_request,echo return value, Fastcgi_finish_request (), followed by the execution .... Or just use the queue.

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