Developing source--php to realize multithreading

Source: Internet
Author: User

Toss for a week, and finally put the task to submit; The task is to implement scheduled tasks and automatic tasks under Linux, which can occur concurrently, so it is necessary to consider multithreading. I also found a lot of information on the Internet does not seem to meet my needs, and finally combined with the wisdom of predecessors and their own research, or achieved;
Public Function Testa () {
$id = $_request[' id '];
for ($i =0; $i <1000000; $i + +) {

}
$time = time ();
$data [' time '] = $time;
M (' Test ')->add ($data);
}

Public Function Testb () {
$id = $_request[' id '];
$time = time ();
$data [' time '] = $time;
M (' Test ')->add ($data);
}
PHP native is not support multi-threaded, if you want the above 2 methods to execute the same time, I used the curl, is the curl parallel these 2 methods of code
This section is set to hide, you have replied, the following is the hidden content
Loading a multi-process curl instance
$MH = Curl_multi_init ();
$handles = Array ();
$array =array ("Localhost:1001/index.php/test/testa", "Localhost:1001/index.php/test/testb");
foreach ($array as $k = $v)
{
Create a single thread curl instance
$ch = Curl_init ();
Set Curl Related parameters
curl_setopt ($ch, Curlopt_url, $v);
curl_setopt ($ch, Curlopt_header, 0);
curl_setopt ($ch, Curlopt_returntransfer, true);
curl_setopt ($ch, Curlopt_timeout, 30);
Load the process into an instance
Curl_multi_add_handle ($MH, $ch);
Join the Loop array
$handles [] = $ch;
}
Performing a Curl multithreaded instance
$running =null;
Do
{
Curl_multi_exec ($MH, $running);
Interval 0.25S
Usleep (250000);
} while ($running > 0);
Get captured Content
for ($i =0; $i <count ($handles); $i + +)
{
Curl_multi_remove_handle ($MH, $handles [$i]);
}
Close an instance
Curl_multi_close ($MH);

Developing source--php to realize multithreading

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.