Curl-how does php implement asynchronous operations without extension?

Source: Internet
Author: User
After receiving the request processing, I have a very time-consuming operation that requires the request interface and need to process many orders to call the interface. You want to perform these operations after echo. Message queue needs to install extensions such as memcacheq for the server. I don't have the right to operate the server and want to solve it with code... after receiving the request processing, I have a very time-consuming operation that requires the request interface and need to process many orders to call the interface. You want to perform these operations after echo. Message queue needs to install extensions such as memcacheq for the server. I don't have the right to operate the server and want to solve it with code. Can I implement a Message Queue? is there any great opportunity to give an example? I have no idea about queue operations. I can also use fsockopen to implement it. what is the difference between it and the message queue mechanism.

Reply content:

After receiving the request processing, I have a very time-consuming operation that requires the request interface and need to process many orders to call the interface. You want to perform these operations after echo. Message queue needs to install extensions such as memcacheq for the server. I don't have the right to operate the server and want to solve it with code. Can I implement a Message Queue? is there any great opportunity to give an example? I have no idea about queue operations. I can also use fsockopen to implement it. what is the difference between it and the message queue mechanism.

// The program is blocked for 10 seconds. shell_exec ('timeout 10 vmstat 1>/dev/null 2> & 1 &'); // The program will not be blocked pclose (popen ('timeout 10 vmstat 1>/dev/null 2> & 1 & ', 'r ')); // therefore, the task pclose (popen ("timeout 60 php/path/to/task. php '$ arg'>/dev/null 2> & 1 & ", 'r '));

The variable $ arg is the parameter passed to the script task. php. this parameter is obtained through $ argv [1] in task. php.
Timeout 60 indicates the maximum execution time of the task. php script is 60 seconds. you can remove it if you do not need it.
The essence of implementing asynchronous pclose (popen () is to open a process to execute blocking code,
This method is applicable to asynchronous scenarios where results (callback) are automatically returned after execution is not required.

The string parameter $ arg can be enclosed in single quotes to avoid the influence of some spaces, but it is still flawed.
To avoid Shell injection (compared with SQL injection), it is best to serialize string parameters to a file using serialize,
Upload the file path parameter to the script task. php to allow task. php to read the unserialize deserialization file and get the data.
The file name should be unique, for example, user id + process PID + time random number:

$filename = md5(uniqid($uid.'_'.getmypid().'_'.mt_rand().'_', true));

Functions provided by PHP-FPMfastcgi_finish_requestYou can flush all the response data to the client and end the request. this allows the client to continue executing code that does not need to be output to the user after the connection ends, such as generating a cache, but still blocking the current FPM working process.
Http://php.net/manual/zh/func...

Fsockopen means to send a request (similar to curl) without waiting for the returned result.

So you can also use fsockopen. after you echo, fsockopen can request local operations on the interface.

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.