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