PHP's questions about implementing multithreading

Source: Internet
Author: User
I want to implement a function in PHP about multithreading. Read multiple data entries from the database first. Then execute the specified function based on an item in the data. Because the algorithm is cumbersome and takes a long time, I want to submit it to the background for processing. But I don't know how to implement it. For example, $ a = newData (& quot; a & quot; PHP's questions about multithreading
I want to implement a function. Read multiple data entries from the database first.

Then execute the specified function based on an item in the data.

Because the algorithm is cumbersome and takes a long time, I want to submit it to the background for processing. But I don't know how to implement it.

For example

$ A = new Data ("");
$ B = new Data ("B ");
I want to directly jump to the execution of the $ B request after sending the $ a request, and so on.
Instead of waiting for the $ a request to complete before the next $ B ..

Is there any good way? The Data function contains multiple parameters ..

------ Solution --------------------
You can create a new php program file, such as runOnBackground. php.
Then, the displayed program is also responsible for asynchronous requests to runOnBackground. php
You can use fsockopen () or socket functions to send asynchronous requests, so that you do not have to wait for the return value. For complex computing on the runOnBackground. php page, you can also submit some parameters.

As for multi-process, it can be used to run in cgi mode and enable pcntl extension in linux, but it is far more complex than the above method.
------ Solution --------------------
The fsockopen example is used in the manual. you only need the code to initiate the request:
$ Fp = fsockopen ("www.example.com", 80, $ errno, $ errstr, 30 );
If (! $ Fp ){
Echo "$ errstr ($ errno)
\ N ";
} Else {
$ Out = "GET/runOnBackground. php? Id = 1 & mode = hello HTTP/1.1 \ r \ n ";
$ Out. = "Host: www.example.com \ r \ n ";
$ Out. = "Connection: Close \ r \ n ";
Fwrite ($ fp, $ out );
}
// Disconnected
Fclose ($ fp );

In runOnBackground. php, you can GET the parameter through $ _ GET ['id'] $ _ GET ['mode '].

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.